[Date Prev][Date Next][Date Index]
[intv] check if string is palindrome.
//Problem: Check if string is palindrome.
#include <iostream>
#include <string.h> //required for strlen
#include <cctype> //required if you want to use toupper.
using namespace std;
bool checkPalin(char *);
int main(){
char *str1 = "Thi is not a palin";
char *str2 = "I prefer Pi";
char *str3 = "A Toyota! Race fast, safe car. A Toyota.";
cout << str1 << " ** " << (checkPalin(str1)?"is":"is not") << " a Palindrome" << endl;
cout << str2 << " ** " << (checkPalin(str2)?"is":"is not") << " a Palindrome" << endl;
cout << str3 << " ** " << (checkPalin(str3)?"is":"is not") << " a Palindrome" << endl;
}
bool checkPalin(char *a){
char *startIdx, *endIdx;
char x, y;
//check if the string is null or contains a single character.
startIdx = a;
endIdx = &a[(strlen(a)-1)];
while(startIdx < endIdx){
x = toupper(*startIdx);
if ((x < 0x41) || (x > 0x5a)){startIdx++; continue;}
y = toupper(*endIdx);
if ((y < 0x41) || (y > 0x5a)){endIdx--; continue;}
if (x != y) {return 0;}
startIdx++; endIdx--;
}
return 1;
}
Comments and corrections are appreciated and can be sent to
papers@mia.ece.uic.edu.
Click here for ©opyright information.
|