import java.util.Scanner; public class Lab_5 { public static void main(String[] args) { /*Question 1. Write a program that prompts the user to enter a string and determines whether it is a palindrome string. A string is palindrome if it reads the same from right to left and from left to right. */ Scanner scanner = new Scanner(System.in); System.out.println("Enter a string:"); String str = scanner.nextLine(); int count = 0; for(int counter =0; counter < str.length(); counter++) { if(str.charAt(counter)==str.charAt(str.length()-1-counter)) count = count + 1; } if( count == str.length()) { System.out.println("The string " + str + " is palindrome"); } else { System.out.println("The string " + str + " is not palindrome"); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter