Reputation: 421
My Problem is, that this code just works with tiny input-strings
The code should check if a input is an Palindrom. And there are two options, i implement them in extra functions (this error also appers, when i comment the functions out)
for example: input "otto" - is okay "reliefpfeiler" - is okay "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" --> segmentation fault
int main(int argc, char **argv)
{
char* str;
int erg;
int c;
char stroriginall[50];
fgets(str,50,stdin);
str[strlen(str)-1]='\0';
if(strlen(str)>40)
{
printf("%s: Error, input must <=40!",argv[0]);
return 1;
}
strcpy(stroriginall,str);
while ((c=getopt(argc, argv, "si")) != -1)
{
switch(c)
{
case 's':
str=removeSpaces(str);
break;
case 'i':
toLowerCase(str);
break;
}
}
erg=checkPalindrom(str);
if(erg==0)
{
printf("%s ist ein Palindrom\n",stroriginall);
}
else
{
printf("%s ist kein Palindrom\n",stroriginall);
}
return 0;
}
i hope anybody can help me :)
Upvotes: 0
Views: 740
Reputation: 18502
One of your problems is that str
is just a pointer (uninitialised for that matter) and you haven't allocated any memory for it. See:
char* str;
int erg;
int c;
char stroriginall[50];
fgets(str,50,stdin);
Change char* str
to char str[50]
. That call to fgets
stores 50 char
s into the buffer pointer to by str
, there's no memory allocated for str
so you'll segfault.
We also don't have the definition to the functions your calling where there could be plenty more problems.
You should also enable all of the warnings on your compiler so these problems are pointed out to you at compile time (if it didn't already do so).
Upvotes: 5