summaryrefslogtreecommitdiff
path: root/algorithms/palindrome.cc
blob: d39382c1a5e1dec000f271be41519a70a4888cc7 (plain)
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
#include <iostream>

using namespace std;


int
chrcmp(char a, char b) {
	if(a == b) { 
		return 0; 
	}
	
	return 1; 
}

int
test_palindrome(string str, int len)
{
	int i=0, j=len-1;
	
	while(i < len-1) {
		if(str[i] == 32) {
			i++;
		} else if(str[j] == 32) {
			j--;
		} else {
			if(chrcmp(str[i], str[j])) {
				return 1;
			}
			i++;
			j--;
		}
	}

	return 0;
}

int main()
{
	string str = "a man a plan a canal panama";
	int len = str.length();
	
	if(test_palindrome(str, len)) {
		cout << "\"" << str << "\" is no palindrome!" << endl;
	}
	else {
		cout << "\"" << str << "\" is a palindrome!" << endl;
	}
}