diff options
author | Patrick Simianer <p@simianer.de> | 2014-06-15 03:24:33 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-06-15 03:24:33 +0200 |
commit | cf3a29feb5887344b6633ead1b4b6d5657a15a4b (patch) | |
tree | f1149508f7305a48dba0226699dfafdd68d81969 /algorithms/palindrome.cc | |
parent | 5ddc763ab9953eebdaf78af4eb72288d7955b310 (diff) |
old stuff: algorithms
Diffstat (limited to 'algorithms/palindrome.cc')
-rw-r--r-- | algorithms/palindrome.cc | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/algorithms/palindrome.cc b/algorithms/palindrome.cc new file mode 100644 index 0000000..d39382c --- /dev/null +++ b/algorithms/palindrome.cc @@ -0,0 +1,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; + } +} + |