Advertisement
shmeri

03.Palindromes

Feb 19th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. System.out.println("Please enter your word : ");
  7. String word = scanner.next().toLowerCase(); //Get user input.
  8. int wordLength = word.length();
  9. int counter = 0;
  10. //Check letters.
  11. for (int i = 0; i < wordLength / 2; i++) {
  12. char firstChar = word.charAt(i);
  13. char lastChar = word.charAt((wordLength-i)-1);
  14. if (firstChar == lastChar){
  15. counter++;
  16. }
  17. }
  18. //Print result.
  19. if (counter == wordLength/2)
  20. {
  21. System.out.println("The word is palindrome!");
  22. }
  23. else{
  24. System.out.println("The word is NOT palindrome!");
  25. }
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement