Spaces check in a Palindrome checking recursive algo.

  • Thread starter Peon666
  • Start date
In summary: I don't remember why it was static, but it's not needed now that I've removed it.In summary, the programmer wants to remove spaces from a string before passing it to a function that checks if the string is a palindrome. However, the function does not work correctly when spaces are present. The programmer tries to fix the function by preprocessing the string, but this does not work. Finally, the programmer tries to fix the function by removing the spaces from the string before passing it to the function.
  • #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:

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.
 
Technology news on Phys.org
  • #2
How about if you preprocessed the string to remove spaces before passing it to your isPalindrome function? A removeSpaces function could take two strings (pointers to char) as parameters, with one string the input string and the other contained all the nonspace characters of the first string.
 
  • #3
Is this a common programming assignment? The similar threads below show that it's been asked about before :smile:.

But yeah, I like the preprocessing idea.
 
  • #4
Alright. Here's a bit modified code and it works right:

Code:
int isPalindrome (char *str)
{
	static int length = strlen (str);
    if (length<1)
		return 1;
        if (str[0] == ' ')
        {
            str++;
            length -= 1;
            return isPalindrome(str);
        }
        if (str[length-1] == ' ')
        {
            length -= 1;
            return isPalindrome(str);
        }
	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;
}

I've encountered another bit of problem, while running the code in VC++ 6.0. It was fine when I was running it in Code Blocks but in VC it gives the following error with this statement:

Code:

static int length = strlen (str);

ERROR: Initializer is not a constant.

But when I remove the 'static', the output is not right. What should I do?
 
  • #5
Does the space removal have to be in the same recursive function? Can you just remove the spaces before feeding it to your isPalindrome?
 
  • #6
It can be in a separate function and spaces can be removed any way.
 
  • #7
Peon666 said:
It can be in a separate function and spaces can be removed any way.

So try that? :smile: If your original function was working except for the spaces, it might be easier to build on if you leave that part relatively intact. Debugging can sometimes be simplified when you keep things "modular" and build in pieces.
 
  • #8
Peon666 said:
Alright. Here's a bit modified code and it works right:

Code:
int isPalindrome (char *str)
{
	static int length = strlen (str);
    if (length<1)
		return 1;
        if (str[0] == ' ')
        {
            str++;
            length -= 1;
            return isPalindrome(str);
        }
        if (str[length-1] == ' ')
        {
            length -= 1;
            return isPalindrome(str);
        }
	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;
}

I've encountered another bit of problem, while running the code in VC++ 6.0. It was fine when I was running it in Code Blocks but in VC it gives the following error with this statement:

Code:

static int length = strlen (str);

ERROR: Initializer is not a constant.

But when I remove the 'static', the output is not right. What should I do?
That was something I noticed before but didn't comment on. Why do you have length being static? I can't think of any good reason for this variable to be static.
 

Related to Spaces check in a Palindrome checking recursive algo.

1. What is a palindrome?

A palindrome is a word, phrase, or sequence that reads the same backward as forward. For example, "racecar" is a palindrome because it reads the same from left to right and right to left.

2. How do you check if a string is a palindrome using recursion?

To check if a string is a palindrome using recursion, the first and last characters of the string are compared. If they are equal, the same process is repeated with the remaining characters until the string is reduced to one character or the middle of the string is reached. If all the characters match, the string is a palindrome.

3. What is the time complexity of a recursive palindrome checking algorithm?

The time complexity of a recursive palindrome checking algorithm is O(n), where n is the length of the string. This is because the algorithm needs to iterate through all the characters in the string to check for a palindrome.

4. Can a recursive algorithm check for spaces in a palindrome?

Yes, a recursive algorithm can check for spaces in a palindrome. The algorithm can be modified to ignore spaces by using conditional statements to skip over them during the comparison process.

5. What are the advantages of using a recursive algorithm for palindrome checking?

One advantage of using a recursive algorithm for palindrome checking is that it is a simple and elegant solution. It also uses less memory compared to iterative approaches. Additionally, recursion allows for easier code maintenance and readability.

Similar threads

  • Programming and Computer Science
Replies
1
Views
953
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
3
Replies
97
Views
7K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
800
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top