summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@iohq.net>2015-10-02 18:14:28 -0600
committerAaron Ball <nullspoon@iohq.net>2015-10-02 18:14:28 -0600
commit41107e2458522a66805ba00428cb42bfea3cce41 (patch)
tree6e46c40aa3c22b2c03cbcf6e8bc8aa99a136d9ff
parent8a02fb11b7addce32f7d488c59fb7cd2b1eda241 (diff)
downloadpalindrome_finder-41107e2458522a66805ba00428cb42bfea3cce41.tar.gz
palindrome_finder-41107e2458522a66805ba00428cb42bfea3cce41.tar.xz
Updated algorithm to handle even numbered palindromes
Was only working with odds, since they have one letter at their center. This change allows for a palindrome to have two letters at its center (even numbered).
-rw-r--r--src/main.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index 20811a7..fcf8c1e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -43,14 +43,36 @@ void find_largest_palindromes(char* data) {
// 0 1 2 3 4 5 6
// r a c e c a r
+ // 0 1 2 3 4 5
+ // a b c c b a
+
while(data[cursor] != '\0') {
+ // Need to align with the center
+ // Advance end one to see if this is an even numbered palindrome
+ if(data[end + 1] != '\0') {
+ ++end;
+ }
+ // If it's not an even numbered, let's see if it's odd numbered
+ if(start > 0 && data[start] != data[end]) {
+ --start;
+ }
+ // Looks like it's not either, reset and continue
+ if(data[start] != data[end]) {
+ ++cursor;
+ start = cursor;
+ end = cursor;
+ continue;
+ }
+
+ // It seems we have found a palindrome, let's see how big...
while(data[start] == data[end]) {
- if(start > 0) {
- --start;
- }
if(data[end + 1] != '\0') {
++end;
}
+ if(start > 0) {
+ --start;
+ }
+
}
if(end - start - 1 > largest) {
largest = end - start - 1;

Generated by cgit