summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@iohq.net>2015-10-01 23:17:06 -0600
committerAaron Ball <nullspoon@iohq.net>2015-10-01 23:17:06 -0600
commit027de39b76558ec5ce6729042c84ab09bf90d7a7 (patch)
treec32735334fb4b5fc403094096913dd079bf4ac9d
downloadpalindrome_finder-027de39b76558ec5ce6729042c84ab09bf90d7a7.tar.gz
palindrome_finder-027de39b76558ec5ce6729042c84ab09bf90d7a7.tar.xz
Initial commit of palindromefinder
-rw-r--r--Makefile6
-rw-r--r--src/main.c136
2 files changed, 142 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..110957b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,6 @@
+out = palindromfinder
+cc = cc
+
+all:
+ $(cc) -O3 src/main.c -o $(out)
+
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..7bdaba0
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,136 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+//
+// Compare the whole thing by passing 0 as end
+//
+// @param str String to check for palindrome
+// @param end For checking a substring shorter than the end of str
+//
+// @return int Is a palindrome (1) or not (0)
+//
+char is_palindrome(char* str, long len) {
+ //long len;
+ long i = 0;
+ long n = len;
+
+ // This handles comparisons of odd and even length strings
+ while(n - i > 0) {
+ if(str[i] == str[n]) {
+ i++;
+ n--;
+ } else {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+
+//
+// Finds the largest palindrome in a data set
+//
+// @param data Data chunk to search for the largest palindrome
+//
+void find_largest_palindromes(char* data) {
+ int largest_start = 0;
+ int largest_end = 0;
+ int largest_len = 0;
+
+ // get length
+ int len = 0;
+ while(data[len] != '\0')
+ len++;
+
+ int start;
+ int end = len;
+ int palindrome;
+ while(end > 0) {
+ // Moving the start index
+ start = 0;
+ while(start < (end - largest_len)) {
+ //printf("testing s: %d e: %d\n", start, end);
+ // Moving the end index
+
+ //printf("%.*s\n", end - start, &data[start]);
+ palindrome = is_palindrome(&data[start], end - start);
+ if(palindrome == 1) {
+ // Update the largest if this one is bigger
+ int curlen = end - start;
+ if(curlen > largest_len) {
+ largest_len = curlen;
+ largest_start = start;
+ largest_end = end;
+ printf("Found new largest: %d chars long\n", largest_len + 1);
+ break;
+ }
+ }
+ start++;
+ }
+ end--;
+ }
+ printf("Longest palindrome is %d long\n", largest_len + 1);
+ printf("It starts at %d and ends at %d\n", largest_start, largest_end);
+ printf("Value: %.*s\n", largest_end - largest_start + 1, &data[largest_start]);
+}
+
+
+//
+// Function for testing one-off strings
+// Useful for debugging
+//
+// @param str String to test for palindromeism
+//
+void palstr(char* str) {
+ if(is_palindrome(str, 0) == 1) {
+ printf("%s \e[1mis\e[0m a palindrome!\n", str);
+ } else {
+ printf("%s is \e[1mnot\e[0m a palindrome.\n", str);
+ }
+}
+
+
+//
+// Ye olde' main function
+//
+int main(int argc, char *argv[]) {
+ if(argc < 2) {
+ printf("Please specify a string to check for palindrome-ism.\n");
+ return 1;
+ }
+
+ FILE *f = fopen(argv[1], "r");
+
+ // If the file can't be opened, try using the passed value as a palindrome
+ // test
+ if(!f) {
+ palstr(argv[1]);
+ return 0;
+ }
+
+ printf("Scanning file %s\n", argv[1]);
+ // Get file length
+ unsigned int flen = 0;
+ while(fgetc(f) != EOF) {
+ flen++;
+ }
+
+ rewind(f);
+
+ printf("Allocating memory\n");
+ // Allocate array for file contents
+ char data[flen + 1];
+ int i = 0;
+ char c;
+ printf("Reading data to memory\n");
+ while((c = fgetc(f)) != EOF) {
+ data[i] = c;
+ i++;
+ }
+
+ fclose(f);
+
+ printf("Searching for palindromes\n");
+ find_largest_palindromes(data);
+ return 0;
+}

Generated by cgit