📜  用于检查字符串是否为回文串的Java程序

📅  最后修改于: 2022-05-13 01:54:32.610000             🧑  作者: Mango

用于检查字符串是否为回文串的Java程序

如果我们从左到右或字符串到左开始读取它,如果它是相同的,则称它为回文。所以让我们考虑一个字符串“ str” ,现在的任务就是找出它的反向字符串是否与它相同。

插图:

Input : str = "abba" 
Output: Yes
Input : str = "geeks"
Output: No  

在上图中,如果我们用ABba代替abba ,那么我们也应该得到输出为yes 。因此,在检查回文之前,将字符串的大小写更改为小写或大写非常重要。如果我们不这样做,我们会得到意想不到的结果。这是因为编译器根据字符的ASCII值检查字符,而AASCII值与a不同。

方法:我们的方法是首先将字符串转换为小写。然后,我们将使用两个指针i指向字符串的开头和j指向字符串的结尾。在i < j时继续增加i并减少j ,并在每一步检查这些指针处的字符是否相同。如果不是,则该字符串不是回文,否则它是。

示例 1:

Java
// Java program to check whether a
// string is a Palindrome
// Using two pointing variables
 
// Main class
public class GFG {
 
    // Method
    // Returning true if string is palindrome
    static boolean isPalindrome(String str)
    {
 
        // Pointers pointing to the beginning
        // and the end of the string
        int i = 0, j = str.length() - 1;
 
        // While there are characters to compare
        while (i < j) {
 
            // If there is a mismatch
            if (str.charAt(i) != str.charAt(j))
                return false;
 
            // Increment first pointer and
            // decrement the other
            i++;
            j--;
        }
 
        // Given string is a palindrome
        return true;
    }
 
    // Method 2
    // main driver method
    public static void main(String[] args)
    {
        // Input string
        String str = "geeks";
 
        //Convert the string to lowercase
        str = str.toLowerCase();
        // passing bool function till holding true
        if (isPalindrome(str))
 
            // It is a palindrome
            System.out.print("Yes");
        else
 
            // Not a palindrome
            System.out.print("No");
    }
}


Java
// Java Program to check Whether the String is Palindrome
// or Not
 
// Main class
class GFG {
 
    // Method 1
    // Returns true if string is a palindrome
    static boolean isPalindrome(String str)
    {
 
        // Pointers pointing to the beginning
        // and the end of the string
        int i = 0, j = str.length() - 1;
 
        // While there are characters to compare
        while (i < j) {
 
            // If there is a mismatch
            if (str.charAt(i) != str.charAt(j))
                return false;
 
            // Increment first pointer and
            // decrement the other
            i++;
            j--;
        }
 
        // Given string is a palindrome
        return true;
    }
 
    // Main driver method
    public static void main(String[] args)
    {
        String str = "geeks";
        String str2 = "RACEcar";
       
        //Change strings to lowercase
        str = str.toLowerCase();
        str2 = str2.toLowerCase();
 
        // For string 1
        System.out.print("String 1 :");
 
        if (isPalindrome(str))
            System.out.print("It is a palindrome");
        else
            System.out.print("It is not a palindrome");
 
        // new line for better readability
        System.out.println();
 
        // For string 2
        System.out.print("String 2 :");
        if (isPalindrome(str2))
            System.out.print("It is a palindrome");
        else
            System.out.print("It is not a palindrome");
    }
}


输出:
No

示例 2:

Java

// Java Program to check Whether the String is Palindrome
// or Not
 
// Main class
class GFG {
 
    // Method 1
    // Returns true if string is a palindrome
    static boolean isPalindrome(String str)
    {
 
        // Pointers pointing to the beginning
        // and the end of the string
        int i = 0, j = str.length() - 1;
 
        // While there are characters to compare
        while (i < j) {
 
            // If there is a mismatch
            if (str.charAt(i) != str.charAt(j))
                return false;
 
            // Increment first pointer and
            // decrement the other
            i++;
            j--;
        }
 
        // Given string is a palindrome
        return true;
    }
 
    // Main driver method
    public static void main(String[] args)
    {
        String str = "geeks";
        String str2 = "RACEcar";
       
        //Change strings to lowercase
        str = str.toLowerCase();
        str2 = str2.toLowerCase();
 
        // For string 1
        System.out.print("String 1 :");
 
        if (isPalindrome(str))
            System.out.print("It is a palindrome");
        else
            System.out.print("It is not a palindrome");
 
        // new line for better readability
        System.out.println();
 
        // For string 2
        System.out.print("String 2 :");
        if (isPalindrome(str2))
            System.out.print("It is a palindrome");
        else
            System.out.print("It is not a palindrome");
    }
}
输出
String 1 :It is not a palindrome
String 2 :It is a palindrome