Task: Jarvis is week in computing palindromes for Alphanumeric characters. While Ironman is busy fighting Thanos, he needs to activate sonic punch but Jarvis is stuck in computing palindromes. Given a string S containing alphanumeric characters. Find out whether the string is a palindrome or not. If string is palindrome print "YES" else print "NO". Iron Man life is in your hands now.
Example:
Input:
1
Ab?/Ba
Output:
YES
Here is the Code:
Program in C++:
- #include<iostream>
- using namespace std;
- string saveIronMan(string s)
- {
- int n = s.length();
- for(int i=0;i<n;i++)
- {
- if(isalpha(s[i])) s[i]=toupper(s[i]);
- }
- int i=0,j=n-1;
- while(i<j)
- {
- while(!isalpha(s[i])&&!isdigit(s[i]))
- {
- if(i<n) i++;
- }
- while(!isalpha(s[j])&&!isdigit(s[j]))
- {
- if(j>-1) j--;
- }
- if(s[i]!=s[j])
- {
- return "NO";
- }
- i++,j--;
- }
- return "YES";
- }
- int main()
- {
- int t;
- scanf("%d\n",&t);
- while(t--)
- {
- string s;
- getline(cin, s);
- cout<<saveIronMan(s)<<endl;
- }
- return 0;
- }
Here is the Video:
Comments
Post a Comment