-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome_Text.java
More file actions
20 lines (19 loc) · 1.07 KB
/
Palindrome_Text.java
File metadata and controls
20 lines (19 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;
public class Palindrome_Text {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the word: ");
String str = sc.nextLine();
sc.close();
String reverse = "";
// Converting the string to lower case so that we can compare both the strings without worrying about the case sensitivity
str = str.toLowerCase();
for (int i = 0; i < str.length(); i++) {
reverse = str.charAt(i) + reverse; // Prepend each character to the reversed string
}
// We are using the for loop to reverse the string after that we are comparing with
// -> inbuilt function equals because we can't use == operator to compare two strings it will give false even if both the strings are same due to diff in reference of teh objects
if (str.equals(reverse)) System.out.println("The given word " + str + " is a Palindrome Text");
else System.out.println("The given word " + str + " is not a Palindrome Text");
}
}