📌  相关文章
📜  检查给定的字符串是否为奇偶回文

📅  最后修改于: 2021-06-26 09:23:25             🧑  作者: Mango

给定字符串str ,任务是检查给定的字符串是否为奇偶回文

例子:

方法:要解决该问题,请通过添加给定字符串的奇数索引字符来创建新字符串,并检查形成的字符串是否为回文。同样,检查偶数索引字符。如果两个字符串都是回文,则打印“是” 。否则,打印“否”
下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if the string
// str is palindromic or not
bool isPalindrome(string str)
{
 
    // Pointers to iterate the
    // string from both ends
    int l = 0;
    int h = str.size() - 1;
 
    while (h > l) {
 
        // If characters are found
        // to be distinct
        if (str[l++] != str[h--]) {
            return false;
        }
    }
 
    // Return true if the
    // string is palindromic
    return true;
}
 
// Function to generate string
// from characters at odd indices
string makeOddString(string str)
{
    string odd = "";
    for (int i = 1; i < str.size();
         i += 2) {
        odd += str[i];
    }
 
    return odd;
}
 
// Function to generate string
// from characters at even indices
string makeevenString(string str)
{
    string even = "";
    for (int i = 0; i < str.size();
         i += 2) {
        even += str[i];
    }
 
    return even;
}
 
// Functions to checks if string
// is Even-Odd Palindrome or not
void checkevenOddPalindrome(string str)
{
 
    // Generate odd indexed string
    string odd = makeOddString(str);
 
    // Generate even indexed string
    string even = makeevenString(str);
 
    // Check for Palindrome
    if (isPalindrome(odd)
        && isPalindrome(even))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
 
// Driver Code
int main()
{
    string str = "abzzab";
 
    checkevenOddPalindrome(str);
    return 0;
}


Java
// Java program implementation
// of the approach
import java.util.*;
import java.io.*;
 
class GFG{
 
// Function to check if the string
// str is palindromic or not
static boolean isPalindrome(String str)
{
 
    // Pointers to iterate the
    // string from both ends
    int l = 0;
    int h = str.length() - 1;
 
    while (h > l)
    {
         
        // If characters are found
        // to be distinct
        if (str.charAt(l++) !=
            str.charAt(h--))
            return false;
    }
 
    // Return true if the
    // string is palindromic
    return true;
}
 
// Function to generate string
// from characters at odd indices
static String makeOddString(String str)
{
    String odd = "";
     
    for(int i = 1; i < str.length(); i += 2)
    {
        odd += str.charAt(i);
    }
 
    return odd;
}
 
// Function to generate string
// from characters at even indices
static String makeevenString(String str)
{
    String even = "";
     
    for(int i = 0; i < str.length(); i += 2)
    {
        even += str.charAt(i);
    }
 
    return even;
}
 
// Functions to checks if string
// is Even-Odd Palindrome or not
static void checkevenOddPalindrome(String str)
{
 
    // Generate odd indexed string
    String odd = makeOddString(str);
 
    // Generate even indexed string
    String even = makeevenString(str);
 
    // Check for Palindrome
    if (isPalindrome(odd) && isPalindrome(even))
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// Driver code
public static void main(String[] args)
{
    String str = "abzzab";
 
    checkevenOddPalindrome(str);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program to implement
# the above approach
 
# Function to check if the string
# str is palindromic or not
def isPalindrome(Str):
 
    # Pointers to iterate the
    # string from both ends
    l = 0
    h = len(Str) - 1
 
    while (h > l):
 
        # If characters are found
        # to be distinct
        if (Str[l] != Str[h]):
            return False
 
        l += 1
        h -= 1
 
    # Return true if the
    # string is palindromic
    return True
 
# Function to generate string
# from characters at odd indices
def makeOddString(Str):
 
    odd = ""
    for i in range(1, len(Str), 2):
        odd += Str[i]
 
    return odd
 
# Function to generate string
# from characters at even indices
def makeevenString(Str):
 
    even = ""
    for i in range(0, len(Str), 2):
        even += Str[i]
 
    return even
 
# Functions to checks if string
# is Even-Odd Palindrome or not
def checkevenOddPalindrome(Str):
 
    # Generate odd indexed string
    odd = makeOddString(Str)
 
    # Generate even indexed string
    even = makeevenString(Str)
 
    # Check for Palindrome
    if (isPalindrome(odd) and
        isPalindrome(even)):
        print("Yes")
    else:
        print("No")
 
# Driver code
Str = "abzzab"
 
checkevenOddPalindrome(Str)
 
# This code is contributed by himanshu77


C#
// C# program implementation
// of the approach
using System;
 
class GFG{
     
// Function to check if the string
// str is palindromic or not
static bool isPalindrome(string str)
{
     
    // Pointers to iterate the
    // string from both ends
    int l = 0;
    int h = str.Length - 1;
 
    while (h > l)
    {
        // If characters are found
        // to be distinct
        if (str[l++] != str[h--])
            return false;
    }
 
    // Return true if the
    // string is palindromic
    return true;
}
 
// Function to generate string
// from characters at odd indices
static string makeOddString(string str)
{
    string odd = "";
     
    for(int i = 1; i < str.Length; i += 2)
    {
        odd += str[i];
    }
 
    return odd;
}
 
// Function to generate string
// from characters at even indices
static string makeevenString(string str)
{
    string even = "";
     
    for(int i = 0; i < str.Length; i += 2)
    {
        even += str[i];
    }
 
    return even;
}
 
// Functions to checks if string
// is Even-Odd Palindrome or not
static void checkevenOddPalindrome(string str)
{
 
    // Generate odd indexed string
    string odd = makeOddString(str);
 
    // Generate even indexed string
    string even = makeevenString(str);
 
    // Check for Palindrome
    if (isPalindrome(odd) && isPalindrome(even))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver code
public static void Main()
{
    string str = "abzzab";
 
    checkevenOddPalindrome(str);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
Yes

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。