-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringPalindrome1.java
43 lines (32 loc) · 1.1 KB
/
StringPalindrome1.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//EXPERIMENT 1
/* write a java program to check whether a STRING is palindrome or not */
import java.util.Scanner;
public class StringPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking input from the user
System.out.print("Enter a string: ");
String originalString = sc.nextLine();
// Convert the string to lowercase to ignore case sensitivity
String str = originalString.toLowerCase();
int len = str.length();
// Reversing the string
String reverseString = "";
for (int i = len - 1; i >= 0; i--) {
reverseString =reverseString+str.charAt(i);
}
// Checking if the original string and reversed string are the same
if (str.equals(reverseString)) {
System.out.println(originalString + " is a palindrome.");
} else {
System.out.println(originalString + " is not a palindrome.");
}
// Closing the scanner
sc.close();
}
}
//SAMPLE OUTPUT
/*
Enter a string: Madam
Madam is a palindrome.
*/