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
51


#include <iostream>
#include <string.h>

using namespace std;

bool isAlphanumeric(char c) {
    return ('a' <= c && c <= 'z')
    || ('A' <= c && c <= 'Z')
    || ('0' <= c && c <= '9');
}

bool isPalindrome(string s) {
    int i = 0;
    int j = s.size() - 1;
    
    while (i < j) {
        if (!isAlphanumeric(s[i])) {
            ++i;
        } else if (!isAlphanumeric(s[j])) {
            --j;
        } else if (s[i] == s[j] 
                   || s[i] - s[j] == 32 
                   || s[j] - s[i] == 32 ) {
            ++i; --j;
        } else {
            return false;
        }
    }
    
    return true;
}


int main (int argc, const char * argv[]) {
    string s = "A man, a plan, a canal: Panama";
    cout << "\"" << s << "\"" << " is " << (isPalindrome(s)? "" : "not ") << "palindrome" << endl;
    
    s = "race a car";
    cout << "\"" << s << "\"" << " is " << (isPalindrome(s)? "" : "not ") << "palindrome" << endl;
    
    s = "Step on hose-pipes? Oh no, pets.";
    cout << "\"" << s << "\"" << " is " << (isPalindrome(s)? "" : "not ") << "palindrome" << endl;
    
    return 0;
}

//Output:
// "A man, a plan, a canal: Panama" is palindrome
// "race a car" is not palindrome
// "Step on hose-pipes? Oh no, pets." is palindrome
View Program Text


Test Status