-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_2.java
More file actions
47 lines (36 loc) · 1.4 KB
/
Problem_2.java
File metadata and controls
47 lines (36 loc) · 1.4 KB
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
44
45
46
47
package strings;
import java.util.Scanner;
//* Problem Title ==> Check String is Palindrome or not
public class Problem_2 {
public static boolean isStringPalindrome(String inputString) {
int startIndex = 0;
int endIndex = inputString.length() - 1;
// Iterate through the string while start and end haven't met
while (startIndex < endIndex) {
char startChar = inputString.charAt(startIndex);
char endChar = inputString.charAt(endIndex);
// Convert characters to lowercase for case-insensitive comparison
startChar = Character.toLowerCase(startChar);
endChar = Character.toLowerCase(endChar);
// If characters are not the same (ignoring case), return false
if (startChar != endChar) {
return false;
}
startIndex++;
endIndex--;
}
// If the loop completes, all characters matched (palindrome)
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
if (isStringPalindrome(inputString)) {
System.out.println("Yes, it is a palindrome.");
} else {
System.out.println("No, it is not a palindrome.");
}
scanner.close();
}
}