Palindrome tester with Java, ignoring spaces and punctuation

后端 未结 7 1361
忘了有多久
忘了有多久 2021-01-07 04:26

I have the program made up until the point where it has to ignore and punctuations and spaces in the thread and I was wondering if anyone could help me with the coding for t

7条回答
  •  天涯浪人
    2021-01-07 04:46

    This code for determine if a word is a palindrome can be much more simplified. Find updated Code

    String word;
    int z;
    int y = 0;
    int i = 0;
    
    char letter;
    
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter a word: ");
    word = input.nextLine();
    
    word = word.replaceAll("\\s+", "");
    word = word.toLowerCase();
    
    z = word.length()-1;
    while (i <= z){
    
        if ((letter = word.charAt(i)) == (letter = word.charAt(z-i))){
            y += 1;
        }
        i += 1;
    }
    
    if (y == (z+1)){
        System.out.println("The word IS a palindrome");
    }
    else{
        System.out.println("The word is NOT a palindrome");
    }
    
    }
    }
    

提交回复
热议问题