summaryrefslogtreecommitdiff
path: root/algorithms/palindrome.cc
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-15 03:24:33 +0200
committerPatrick Simianer <p@simianer.de>2014-06-15 03:24:33 +0200
commitcf3a29feb5887344b6633ead1b4b6d5657a15a4b (patch)
treef1149508f7305a48dba0226699dfafdd68d81969 /algorithms/palindrome.cc
parent5ddc763ab9953eebdaf78af4eb72288d7955b310 (diff)
old stuff: algorithms
Diffstat (limited to 'algorithms/palindrome.cc')
-rw-r--r--algorithms/palindrome.cc49
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;
+ }
+}
+