- #1
Peon666
- 108
- 0
Hi all.
I have a little problem with a code that checks whether an input string is a palindrome or not. But my problem is that it does not check for the spaces, and I want it to do so.
Here's the code:
For example, if the input string is:
a man a plan a canal panama
It says that this is NOT a palindrome (spaces problem). I'd be really helpful if anyone sorts this out for me.
Thanks.
I have a little problem with a code that checks whether an input string is a palindrome or not. But my problem is that it does not check for the spaces, and I want it to do so.
Here's the code:
Code:
#include <stdio.h>
#include <string.h>
int isPalindrome (char *str)
{
static int length = strlen (str);
if (length<1)
return 1;
if (str[0] == str[length-1])
{
length -= 2;
return isPalindrome (str+1);/*Recursive call as the function isPalindrome
is called again within itself*/
}
else return 0;
}
int main (void)
{
int result;
char str[256];
printf ("\nPlease type a string: \n");
gets (str);/*Input a string to check whether it is a palindrome or not*/
result = isPalindrome (str);/*The function isPalindrome is called.It takes a string
argument and returns a value 0 or 1 which is stored in
the integer variable "result"*/
if (result==1)
printf ("\n******Input string is a palindrome string.************\n");
else
printf ("\n******Not a palindrome******\n");
return 1;
}
For example, if the input string is:
a man a plan a canal panama
It says that this is NOT a palindrome (spaces problem). I'd be really helpful if anyone sorts this out for me.
Thanks.