C++ program
checks if an input string is a palindrome or not. Ignore case and remove all non-alphanumeric characters in the input string.
C++ program uses only one string (no extra-string or array). And also, use two indices to point from the beginning and ending positions.
Sample Input 0
Race car
Sample Output 0
TRUE
Sample Input 1
7...8 Don't nod 78.
Sample Output 1
FALSE
#include<iostream> using namespace std; bool isPalindrome(string s){ int i,j; for(i = 0; s[i]!='\0'; i++){ s[i] = tolower(s[i]); } string alphaNum = ""; for(i = 0;s[i]!='\0';i++){ if((s[i]>='a' && s[i]<='z') || (s[i]>='0' && s[i]<='9')) alphaNum += s[i]; } i = 0; j = alphaNum.length()-1; while(i<j){ if(alphaNum[i] != alphaNum[j]){ return false; } i++; j--; } return true; } int main() { string str; getline(cin,str); if(isPalindrome(str)){ cout<<"TRUE"<<endl; } else{ cout<<"FALSE"<<endl; } return 0; }
Get Answers For Free
Most questions answered within 1 hours.