-
Notifications
You must be signed in to change notification settings - Fork 5
/
Alphanumeric_Palindrome.java
50 lines (44 loc) · 1.69 KB
/
Alphanumeric_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
/*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.*;
public class Alphanumeric_Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.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);
}
}
//System.out.println(Arrays.toString(ch));
boolean check = true;
for (int i = 0; i < ch.length; i++) {
if(ch[i] != ch[ch.length-1-i])
{
check = false;
break;
}
}
if(check == true)
{
System.out.println("True");
}
else
{
System.out.println("False");
}
}
}