I already tried Valgrind with the Valkyrie GUI and Code::Blocks, but I did not really know how to create a project to run it in the debugger, or it just did not work. The printf("here")
lines were just for checking were the error was, however, it just sometimes returned "here4". It always returned Segmentation fault: 11, and I cannot see any line where I am adressing memory I do not own.
#include <stdio.h>
#include <string.h>
int c, length=-1, n, m, palindrome, line[250];
int half[126];
int half2[126];
char string[250];
char charac[2];
int main(){
while(c!=EOF){
c=getchar();
if(c=='\n' || c==EOF){ /*End of line*/
/*Copies first half of line*/
while(n<=length/2){
half[n]=line[n];
++n;
}
/*Copies second half of line*/
for(n=0, m=length; n<=length/2; ++n, --m){
half2[n]=line[m];
}
/*Tests if line is palindrome*/
for(palindrome=1, n=0; palindrome && n<=length/2; ++n){
if(half[n]!=half2[n])
palindrome=0;
}
if(palindrome)
printf("%s\nis a palindrome.\n", string);
length=-1;
n=0;
m=0;
string[0]=0;
while(n<=125){
half[n]=0;
half2[n]=0;
++n;
}
n=0;
}
else{
/*printf("here4");*/
++length;
if(length>500)
break; /*prevent overflow*/
line[length]=c;
sprintf(charac, "%c", c);
strcat(string, charac);
}
}
return 0;
}