I have to write a function to check for palindromes in sentences and words.
For sentences it looks like this (and works):
int checkSentencepalindrome(char * str)
{
size_t len = strlen(str);
char *pntr1 = str;
char *pntr2 = str + len - 1;
while(pntr2 >= pntr1)
{
if (!isalpha(*pntr2))
{
pntr2--;
continue;
}
if (!isalpha(*pntr1))
{
pntr1++;
continue;
}
if(tolower(*pntr1) != tolower(*pntr2))
{
return 0;
}
pntr1++;
pntr2--;
}
return 1;
}
for word palindromes it looks like this:
int checkWordpalindrome(char * str)
{
size_t len = strlen(str);
char *pntr1 = str;
char *pntr2 = str + len - 1;
while(pntr2 >= pntr1)
{
if(tolower(*pntr1) != tolower(*pntr2))
{
return 0;
}
pntr1++;
pntr2--;
}
return 1;
}
However, the function for word palindromes returns me 0 all the time. (I expect a 1 for palindrome and 0 for non-palindrome words)
I thought the if statements I deleted are just there to skip spaces, exclamation marks, etc. (everything not included in the alphabet) Why are they so crucial for my function to work properly then?
How can I solve this while using pointers only?
EDIT: The issue only occurs when passing the string as following:
int checkWpalindrome(char * str)
{
printf("%s ", str);
size_t len = strlen(str);
if(len == 0)
{
return 0;
}
char *pntr1 = str;
char *pntr2 = str + len - 1;
while(pntr2 >= pntr1)
{
if(tolower(*pntr1) != tolower(*pntr2))
{
return 0;
}
pntr1++;
pntr2--;
}
return 1;
}
void input(char * str)
{
printf("Input: ");
fgets(str, 101, stdin);
fflush(stdin);
}
int main()
{
char arr[10];
input(arr);
printf("%d", checkWpalindrome(arr));
return 0;
}
fgets()
includes reading and saving a'\n'
and causescheckWordpalindrome()
to return 0 (unless maybe only"\n"
was read). @Adrian Mole.To lop off the potential
'\n'
, usestr[strcspn(str, "\n")] = 0;
right afterfgets()
.Both functions risk undefined behavior (UB) as they can attempt to generate a pointer to before
str
. Considercheck...("?")
.In
checkSentencepalindrome()
the UB is is more so as code then attempts*pntr2
of an invalid pointer.isalpha(ch)
andtolower(ch)
have (UB) whench < 0
(and notEOF
)Calling code buffer too small:
char arr[10]; ... fgets(str, 101, stdin);
. Make them the same size likechar arr[101]; ... fgets(str, 101, stdin);
fflush(stdin);
is UB and not needed. @Adrian MoleRepaired code:
A bit tidier with