Home > front end >  Check palindrome of string array
Check palindrome of string array

Time:03-08

Given this array. find all palindrome string String[] names = {"Anna", "Nalan", "Hannah", "Emma", "Aidai", "Ciris", "Yasin", "Bob", "Inna", "Eve"};

Can anyone help me fixing my error?

 String[] names = {"Anna", "Nalan", "Hannah", "Emma", "Aidai", "Ciris", "Yasin", "Bob", "Inna", "Eve"};
    boolean isPalindrome = true;
    String reverses[] = new String[names.length];
    String reverse = "";
    char [] ch = {};

// System.out.println(Arrays.toString(names));

    for (int i = 0; i< names.length; i  ){
        System.out.println(names[i]);//convert from array to string
        int strLength = names[i].length();
        System.out.println(strLength);

        for(int j = strLength - 1; j>=0; j--) {
            reverse = reverse   names[i].charAt(j);
        }
        if (names[i].toLowerCase().equals(reverse.toLowerCase())){
            System.out.println(reverse);

Output is:

Anna
4
annA
Nalan
5
Hannah
6
Emma
4
Aidai
5
Ciris
5
Yasin
5
Bob
3
Inna
4
Eve
3

CodePudding user response:

A different approach:

String[] names = {"Anna", "Nalan", "Hannah", "Emma", "Aidai", "Ciris", "Yasin", "Bob", "Inna", "Eve"};
for (String name : names) {
  System.out.print(name);
  
  boolean isPalindrome = true;
  for(int x=0, y=name.length()-1; x<y && isPalindrome; x  , y--) {
    isPalindrome=(name.substring(x,x 1).equalsIgnoreCase(name.substring(y,y 1)));
  }
  
  if (isPalindrome){
      System.out.println(" is a Palindrome");
  }
  else {
    System.out.println(" is NOT a Palindrome");
  }
} 

Output:

Anna is a Palindrome
Nalan is a Palindrome
Hannah is a Palindrome
Emma is NOT a Palindrome
Aidai is NOT a Palindrome
Ciris is NOT a Palindrome
Yasin is NOT a Palindrome
Bob is a Palindrome
Inna is NOT a Palindrome
Eve is a Palindrome

CodePudding user response:

public static void main(String[] args) {                                                                                                                                                        
    String[] names = {"Anna", "Nalan", "Hannah", "Emma", "Aidai", "Ciris", "Yasin", "Bob", "Inna", "Eve"};
                                                                            
    for (String str : names) {                                              
        String lowStr = str.toLowerCase();                                  
        char[] chs = lowStr.toCharArray();                                  
        for ( int i = 0, j = chs.length -1; i < j; ) {                      
            char c = chs[i];                                                  
            chs[i  ] = chs[j];                                              
            chs[j--] = c;                                                   
        }                                                                     
        if ( lowStr.equals(new String(chs)) ) {                             
            System.out.println(str);                                        
            System.out.println(str.length());                               
        }                                                                   
    }                                                                       
}

CodePudding user response:

With StringBuilder to easily find all palindrome string in String[].

Running Code -

class PalindromeStrings {

    private static void printOnlyPalindrome(String str) {
        String oldString = str;
        StringBuilder builder = new StringBuilder(str);
        if (builder.reverse().toString().toLowerCase().equals(oldString.toLowerCase())) {
            System.out.println(oldString);
        }
    }

    public static void main(String[] args) {
        String stringArray[] = {
            "Anna",
            "Nalan",
            "Hannah",
            "Emma",
            "Aidai",
            "Ciris",
            "Yasin",
            "Bob",
            "Inna",
            "Eve"
        };
        for (int i = 0; i < stringArray.length; i  ) {
            printOnlyPalindrome(stringArray[i]);
        }
    }


}
  • Related