Skip to main content

Save Iron Man

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++:

  1. #include<iostream>
  2. using namespace std;
  3. string saveIronMan(string s)
  4. {
  5.     int n = s.length();
  6.     for(int i=0;i<n;i++)
  7.     {
  8.         if(isalpha(s[i])) s[i]=toupper(s[i]);
  9.     }
  10.     int i=0,j=n-1;
  11.     while(i<j)
  12.     {
  13.         while(!isalpha(s[i])&&!isdigit(s[i]))
  14.         {
  15.             if(i<n) i++;
  16.         }
  17.         while(!isalpha(s[j])&&!isdigit(s[j]))
  18.         {
  19.             if(j>-1) j--;
  20.         }
  21.         if(s[i]!=s[j])
  22.         {
  23.             return "NO";
  24.         }
  25.         i++,j--;
  26.     }
  27.     return "YES";
  28. }
  29. int main()
  30.  {
  31. int t;
  32. scanf("%d\n",&t);
  33. while(t--)
  34. {
  35.     string s;
  36.     getline(cin, s);
  37.     cout<<saveIronMan(s)<<endl;
  38. }
  39. return 0;
  40. }


Here is the Video: