-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindrome.java
62 lines (42 loc) · 1.83 KB
/
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*Write a program to check if a given string is a valid palindrome, considering only alphanumeric characters and ignoring cases.
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.
Your program should return True if the input string is a valid palindrome, and False otherwise.
For example, if the input string is "A man, a plan, a canal: Panama", the program should return True, as the string is a
valid palindrome when considering only alphanumeric characters ("amanaplanacanalpanama").
Write a program that takes a string as input and determines whether it is a valid palindrome or not, considering alphanumeric
characters and ignoring cases.
Sample input: "A man, a plan, a canal: Panama"
Sample output: True
Sample input: "hello world"
Sample output: False
*/
import java.util.Arrays;
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
System.out.println("Enter String");
Scanner in = new Scanner(System.in);
String s= in.nextLine().toLowerCase();
char[] ch = new char[0];
for (int i = 0; i < s.length(); i++) {
// if(Character.isLetter(s.charAt(i)))
// {
ch = Arrays.copyOf(ch, ch.length+1);
ch[ch.length-1] = s.charAt(i);
}
boolean check = true;
// System.out.println(Arrays.toString(ch));
for (int i = 0; i < ch.length; i++) {
if(ch[i] != ch[ch.length-1-i]){
check = false;
break;
}
}
if (check) {
System.out.println("True");
// System.out.println(Arrays.toString(ch));
} else {
System.out.println("False");
}
}
}