Solved

# recursive problem

Posted on 2008-10-06
244 Views
I have a palindrome problem that I have to write recurrsively. I have the code and it works but it is not recurrsive and I need it to be recursive.
``````#include <stdio.h>

#include <string.h>

#define TRUE 1

#define FALSE 0

main ()

{

char decision;

char str[30];

int isPalindrome = TRUE;

int choice = TRUE;

int i,j;

printf("Enter a word: ");

gets(str);

j = strlen(str) - 1;

i = 0;

while(i <= j && isPalindrome)

{

if(str[i] != str[j])

{

isPalindrome = FALSE;

}

i++;

j--;

}

if(isPalindrome)

{

printf("%s is a palindrome!\n", str);

}

else

{

printf("%s is not a palindrome\n", str);

}

printf("Would you like to enter another word? y or n \n");

scanf("%c", &decision);

if (decision == 'y') choice = TRUE;

else choice = FALSE;

}

}
``````
0
Question by:mikeregas

LVL 9

Expert Comment

ID: 22654566

``````<Deleted>
``````
0

LVL 53

Expert Comment

ID: 22654774
@mgonullu : Don't post full code for what is likely an assignment. That is not allowed on EE.

@mikeregas : please ignore mgonullu's post, and try to solve this yourself. We'll be here to assist you with guidance, hints, and answers to specific questions.

For now, can you explain what it is exactly that you have a problem with ? Do you understand what recursive functions are and how to use them ? Is your problem somewhere else ? Did you give this a try yet ? If so, can you post what you came up with ?
0

LVL 45

Accepted Solution

Kdo earned 500 total points
ID: 22655713
Hi Mike,

Just couldn't get enough of recursion, could you.  :)

The previous problem dealt with permutations of binary sets.  There was a simple iterative solution, but the assignment was to solve it recursively.  By the way, the easiest linear for the earlier problem is to use an integer it be a bitmask for each flip.  The only limitation is that *N* must be smaller than the number of bits in in integer, but since you're unlikely to print 2 billion lines, that's not that big a limitation.  :)

1 initialize a counter to 0
2 Evaluate the bits (from the right) bits 0 to (N-1).  Where 0 print 'T', where 1, print 'H'
3 Add 1 to the counter
4 if the counter < 2^N, go to 2

This problem is similar in that there's a very good iterative solution, but a recursive solution is also quite handy.

To build a recursive function to test that the string is a palindrome (and why didn't the coiner of the word 'palindrome' make it one?) you'll need two pointers.  One is the first character in the string, the other is the last character in the string.

You then call the function, passing those two parameters.  The function must test to make sure that the addresses do not overlap.  If the two addresses are the same, there was an odd number of characters and the string passed the palindrome test.  If the first (smaller) address has been incremented so that it's now larger than the second address, the string has an even number of characters and it has passed the palindrome test.

If neither of those events is true, test the characters at the two addresses.  If they do not match, the string is not a palindrome.  If they do match, increment the first address, decrement the second address, and recursively call the function with the two new addresses.

That's all there is to it.  :)

Good Luck,
Kent
0

## Featured Post

### Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.