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 🙂
One of your problems is that
stris just a pointer (uninitialised for that matter) and you haven’t allocated any memory for it. See:Change
char* strtochar str[50]. That call tofgetsstores 50chars into the buffer pointer to bystr, there’s no memory allocated forstrso 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).