-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathString_Palindrome.java
26 lines (20 loc) · 1.09 KB
/
String_Palindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner;
class Palindrome_String {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String inputFromUser = myObj.nextLine(); // Reads the input and stores it in str
String reverseInput = "";
int strLength = inputFromUser.length(); // Assigns length of input string to variable
for (int i = (strLength - 1); i >= 0; --i) { // loop will execute till the end of the string
reverseInput = reverseInput + inputFromUser.charAt(i);
}
if (inputFromUser.toLowerCase().equals(reverseInput.toLowerCase())) { // checks if both reversed string and
// orignal string is equal
System.out.println(inputFromUser + " is a Palindrome String."); // print if its a palindrome
} else {
System.out.println(inputFromUser + " is not a Palindrome String."); // print if it is not palindrome
}
myObj.close();
}
}