Last active
September 17, 2015 08:10
-
-
Save kahless62003/e5b580b46f8d5dc6046e to your computer and use it in GitHub Desktop.
r dailyprogrammer 20150914 Challenge 232 Easy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#include <string.h> | |
#include <limits.h> | |
/*Finds length of longest line in file*/ | |
int charcount(char *filename) | |
{ | |
FILE *fp; | |
int count = 0, maxcount = 0; | |
char c; | |
fp = fopen (filename, "r"); | |
if (fp == NULL) | |
{ | |
fputs ("Failed to open file.\n", stderr); | |
return 0; | |
} | |
do | |
{ | |
c = fgetc( fp ); | |
if( c == '\n') | |
{ | |
if( count > maxcount) | |
maxcount=count; | |
count = 0; | |
} | |
if (count+1<INT_MAX) | |
{ | |
count++; | |
} | |
else | |
{ | |
fputs ("Integer about to overrun. Aborting.\n", stderr); | |
return 0; | |
} | |
} | |
while (c!=EOF); | |
fclose(fp); | |
return maxcount; | |
} | |
/*Finds number of lines in file*/ | |
int linecount(char *filename) | |
{ | |
FILE *fp; | |
int count = 0; | |
char c; | |
fp = fopen (filename, "r"); | |
if (fp == NULL) | |
{ | |
fputs ("Failed to open file.\n", stderr); | |
return 0; | |
} | |
else | |
{ | |
do | |
{ | |
c = fgetc( fp ); | |
if( c == '\n') | |
{ | |
if (count+1<INT_MAX) | |
{ | |
count++; | |
} | |
else | |
{ | |
fputs ("Integer about to overrun. Aborting.\n", stderr); | |
fclose(fp); | |
return 0; | |
} | |
} | |
} | |
while (c!=EOF); | |
fclose(fp); | |
} | |
return count; | |
} | |
/*Finds number of lines in buffer*/ | |
int how_many_lines_in_input(char *buffer) | |
{ | |
int counter=0, lc0, length; | |
if ((long int)strlen(buffer)<(long int)INT_MAX) | |
{ | |
length=(int)strlen(buffer); | |
} | |
if (length>0) | |
{ | |
for(lc0=0; lc0<=length; lc0++) | |
{ | |
if (lc0<length && buffer[lc0]=='\n') | |
counter++; | |
} | |
counter++; | |
return counter; | |
} | |
else | |
return 0; | |
} | |
/*Finds whether the text content of a buffer is a palindrome*/ | |
int is_input_a_palindrome(char *buffer) | |
{ | |
int counter=0, lc0, length_of_buffer=0, length_of_only_text_from_buffer=0, low_index, high_index; | |
char *only_text_from_buffer; | |
char ch; | |
if ((long int)strlen(buffer)<(long int)INT_MAX) | |
{ | |
length_of_buffer=(int)strlen(buffer); | |
} | |
if (length_of_buffer>0) | |
{ | |
only_text_from_buffer=malloc( length_of_buffer+2 * sizeof(char *)); | |
if(only_text_from_buffer==NULL) | |
{ | |
fputs("Error: memory not allocated for buffer.\n", stderr); | |
return 2; | |
} | |
else | |
{ | |
only_text_from_buffer[0]='\0'; | |
//strip away everything that is not a letter and store in new buffer | |
for(lc0=0; lc0<=length_of_buffer; lc0++) | |
{ | |
ch=buffer[lc0]; | |
if (lc0<length_of_buffer && ch!='\n' && isalpha(ch)!=0) | |
{ | |
only_text_from_buffer[counter]=tolower(ch); | |
counter++; | |
only_text_from_buffer[counter]='\0'; | |
} | |
} | |
if(strlen(only_text_from_buffer)>0) | |
{ | |
//Now actually do the work of checking for a palindrome. | |
length_of_only_text_from_buffer=(int)strlen(only_text_from_buffer); | |
low_index=0; | |
high_index=length_of_only_text_from_buffer-1; | |
while(high_index > low_index) | |
{ | |
if (only_text_from_buffer[low_index++] != only_text_from_buffer[high_index--]) | |
{ | |
free(only_text_from_buffer); | |
return 1; | |
} | |
} | |
} | |
free(only_text_from_buffer); | |
return 0; | |
} | |
} | |
else | |
return 2; | |
} | |
/*Processes the wordlist for palindromes*/ | |
int process_wordlist(char *filename) | |
{ | |
FILE *fp1, *fp2; | |
int count = 0; | |
int maxlinelength, maxlinecount; | |
char *buffer, *buffer2, *combined_buffer; | |
printf("Processing pairs of words from /r/dailyprogrammer's favourite word list...\n"); | |
maxlinelength=charcount(filename); | |
maxlinecount=linecount(filename); | |
if (maxlinelength > 0 && maxlinecount > 0) | |
{ | |
buffer=malloc( maxlinelength+2 * sizeof(char *)); | |
if(buffer==NULL) | |
{ | |
fputs("Error: memory not allocated for buffer.\n", stderr); | |
return 1; | |
} | |
buffer2=malloc( maxlinelength+2 * sizeof(char *)); | |
if(buffer2==NULL) | |
{ | |
fputs("Error: memory not allocated for buffer.\n", stderr); | |
return 1; | |
} | |
combined_buffer=malloc( maxlinelength+maxlinelength+2 * sizeof(char *)); | |
if(combined_buffer==NULL) | |
{ | |
fputs("Error: memory not allocated for buffer.\n", stderr); | |
return 1; | |
} | |
fp1 = fopen (filename, "r"); | |
if (fp1 == NULL) | |
{ | |
fputs ("Failed to open file 1st time.\n", stderr); | |
free(buffer); | |
free(buffer2); | |
free(combined_buffer); | |
return 1; | |
} | |
while(fgets(buffer, maxlinelength+2, fp1) != NULL) | |
{ | |
buffer[strlen(buffer)-2]='\0'; | |
fp2 = fopen (filename, "r"); | |
if (fp2 == NULL) | |
{ | |
fputs ("Failed to open file 2nd time.\n", stderr); | |
free(buffer); | |
free(buffer2); | |
free(combined_buffer); | |
return 1; | |
} | |
while(fgets(buffer2, maxlinelength+2, fp2) != NULL) | |
{ | |
buffer2[strlen(buffer2)-2]='\0'; | |
if (strcmp(buffer, buffer2) != 0) | |
{ | |
strcpy(combined_buffer, buffer); | |
strcat(combined_buffer, buffer2); | |
if(combined_buffer[0]==combined_buffer[strlen(combined_buffer)-1])// && combined_buffer[1]==combined_buffer[strlen(combined_buffer)-2] | |
{ | |
if(is_input_a_palindrome(combined_buffer)==0) | |
{ | |
if (count+1<INT_MAX) | |
{ | |
count++; | |
printf("%s %s is a Palindrome.\n", buffer, buffer2); | |
} | |
else | |
{ | |
fputs ("Integer about to overrun. Aborting.\n", stderr); | |
free(buffer); | |
free(buffer2); | |
free(combined_buffer); | |
fclose(fp2); | |
fclose(fp1); | |
return 0; | |
} | |
} | |
} | |
} | |
} | |
fclose(fp2); | |
} | |
fclose(fp1); | |
printf("\nThere were %i counted palindromes.\n", count); | |
free(buffer); | |
free(buffer2); | |
free(combined_buffer); | |
} | |
return count; | |
} | |
/*Processes the input file for palindromes*/ | |
int process_input_file(char *filename) | |
{ | |
FILE *fp; | |
int number_of_lines_to_process = 0, lc0 = 0; | |
int maxlinelength, maxlinecount; | |
char *buffer, *combined_buffer; | |
printf("Processing test and challenge inputs as requested...\n"); | |
maxlinelength=charcount(filename); | |
maxlinecount=linecount(filename); | |
if (maxlinelength > 0 && maxlinecount > 0) | |
{ | |
buffer=malloc( maxlinelength+2 * sizeof(char *)); | |
if(buffer==NULL) | |
{ | |
fputs("Error: memory not allocated for buffer.\n", stderr); | |
return 1; | |
} | |
fp = fopen (filename, "r"); | |
if (fp == NULL) | |
{ | |
fputs ("Failed to open file.\n", stderr); | |
return 1; | |
} | |
while(fgets(buffer, maxlinelength+2, fp) != NULL) | |
{ | |
if(buffer[strlen(buffer)-1]=='\n') | |
buffer[strlen(buffer)-1]='\0'; | |
if( | |
(strlen(buffer)==1 && isdigit(buffer[0])!=0) || | |
(strlen(buffer)==2 && isdigit(buffer[0])!=0 && isdigit(buffer[1])!=0) | |
) | |
{ | |
number_of_lines_to_process=atoi(buffer); | |
printf("number_of_lines_to_process= %i\n", number_of_lines_to_process); | |
combined_buffer=malloc( ((maxlinelength * number_of_lines_to_process)+2) * sizeof(char *)); | |
if(combined_buffer==NULL) | |
{ | |
fputs("Error: memory not allocated for buffer.\n", stderr); | |
return 1; | |
} | |
combined_buffer[0]='\0'; | |
for(lc0=1; lc0<=number_of_lines_to_process; lc0++) | |
{ | |
if(fgets(buffer, maxlinelength+2, fp) != NULL) | |
strcat(combined_buffer, buffer); | |
} | |
printf("%s\n", combined_buffer); | |
if(is_input_a_palindrome(combined_buffer)==0) | |
printf("Palindrome.\n\n"); | |
else if(is_input_a_palindrome(combined_buffer)==1) | |
printf("Not a Palindrome.\n\n"); | |
combined_buffer[0]='\0'; | |
} | |
free(combined_buffer); | |
} | |
fclose(fp); | |
free(buffer); | |
} | |
return 0; | |
} | |
int main(int argc, char **argv) | |
{ | |
char filename_input[]="test_challenge_input.txt"; | |
char filename_test[]="test_enable1.txt"; | |
char filename_live[]="enable1.txt"; | |
char test_input1[]="Was it a car\nor a cat\nI saw?"; | |
char test_input2[]="A man, a plan, \na canal, a hedgehog, \na podiatrist, \nPanama!"; | |
char test_input3[]="A man, a plan, \na canal, \nPanama!"; | |
char challenge_input1[]="Are we not drawn onward, \nwe few, drawn onward to new area?"; | |
char challenge_input2[]="Dammit I'm mad.\nEvil is a deed as I live.\nGod, am I reviled? I rise, my bed on a sun, I melt.\nTo be not one man emanating is sad. I piss.\nAlas, it is so late. Who stops to help?\nMan, it is hot. I'm in it. I tell.\nI am not a devil. I level \"Mad Dog\".\nAh, say burning is, as a deified gulp,\nIn my halo of a mired rum tin.\nI erase many men. Oh, to be man, a sin.\nIs evil in a clam? In a trap?\nNo. It is open. On it I was stuck.\nRats peed on hope. Elsewhere dips a web.\nBe still if I fill its ebb.\nEw, a spider... eh?\nWe sleep. Oh no!\nDeep, stark cuts saw it in one position.\nPart animal, can I live? Sin is a name.\nBoth, one... my names are in it.\nMurder? I'm a fool.\nA hymn I plug, deified as a sign in ruby ash,\nA Goddam level I lived at.\nOn mail let it in. I'm it.\nOh, sit in ample hot spots. Oh wet!\nA loss it is alas (sip). I'd assign it a name.\nName not one bottle minus an ode by me:\n\"Sir, I deliver. I'm a dog\"\nEvil is a deed as I live.\nDammit I'm mad."; | |
int temp, temp2; | |
if(argc > 1 && (strcmp(argv[1], filename_input) == 0)) | |
{ | |
process_input_file(argv[1]); | |
} | |
if(argc > 1 && (strcmp(argv[1], filename_test) == 0 || strcmp(argv[1], filename_live) == 0) ) | |
{ | |
process_wordlist(argv[1]); | |
} | |
else if (argc == 1) | |
{ | |
temp=how_many_lines_in_input(test_input1); | |
if (temp>0) | |
{ | |
printf("\n%i\n%s\n\n", temp, test_input1); | |
temp2=is_input_a_palindrome(test_input1); | |
if(temp2==0) | |
printf("Palindrome.\n\n"); | |
else if(temp2==1) | |
printf("Not a Palindrome.\n\n"); | |
} | |
temp=how_many_lines_in_input(test_input2); | |
if (temp>0) | |
{ | |
printf("\n%i\n%s\n\n", temp, test_input2); | |
temp2=is_input_a_palindrome(test_input2); | |
if(temp2==0) | |
printf("Palindrome.\n\n"); | |
else if(temp2==1) | |
printf("Not a Palindrome.\n\n"); | |
} | |
temp=how_many_lines_in_input(test_input3); | |
if (temp>0) | |
{ | |
printf("\n%i\n%s\n\n", temp, test_input3); | |
temp2=is_input_a_palindrome(test_input3); | |
if(temp2==0) | |
printf("Palindrome.\n\n"); | |
else if(temp2==1) | |
printf("Not a Palindrome.\n\n"); | |
} | |
temp=how_many_lines_in_input(challenge_input1); | |
if (temp>0) | |
{ | |
printf("\n%i\n%s\n\n", temp, challenge_input1); | |
temp2=is_input_a_palindrome(challenge_input1); | |
if(temp2==0) | |
printf("Palindrome.\n\n"); | |
else if(temp2==1) | |
printf("Not a Palindrome.\n\n"); | |
} | |
temp=how_many_lines_in_input(challenge_input2); | |
if (temp>0) | |
{ | |
printf("\n%i\n%s\n\n", temp, challenge_input2); | |
temp2=is_input_a_palindrome(challenge_input2); | |
if(temp2==0) | |
printf("Palindrome.\n\n"); | |
else if(temp2==1) | |
printf("Not a Palindrome.\n\n"); | |
} | |
} | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 | |
Was it a car | |
or a cat | |
I saw? | |
4 | |
A man, a plan, | |
a canal, a hedgehog, | |
a podiatrist, | |
Panama! | |
3 | |
A man, a plan, | |
a canal, | |
Panama! | |
2 | |
Are we not drawn onward, | |
we few, drawn onward to new area? | |
29 | |
Dammit I'm mad. | |
Evil is a deed as I live. | |
God, am I reviled? I rise, my bed on a sun, I melt. | |
To be not one man emanating is sad. I piss. | |
Alas, it is so late. Who stops to help? | |
Man, it is hot. I'm in it. I tell. | |
I am not a devil. I level "Mad Dog". | |
Ah, say burning is, as a deified gulp, | |
In my halo of a mired rum tin. | |
I erase many men. Oh, to be man, a sin. | |
Is evil in a clam? In a trap? | |
No. It is open. On it I was stuck. | |
Rats peed on hope. Elsewhere dips a web. | |
Be still if I fill its ebb. | |
Ew, a spider... eh? | |
We sleep. Oh no! | |
Deep, stark cuts saw it in one position. | |
Part animal, can I live? Sin is a name. | |
Both, one... my names are in it. | |
Murder? I'm a fool. | |
A hymn I plug, deified as a sign in ruby ash, | |
A Goddam level I lived at. | |
On mail let it in. I'm it. | |
Oh, sit in ample hot spots. Oh wet! | |
A loss it is alas (sip). I'd assign it a name. | |
Name not one bottle minus an ode by me: | |
"Sir, I deliver. I'm a dog" | |
Evil is a deed as I live. | |
Dammit I'm mad. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment