• C

recursive problem

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: ");
	j = strlen(str) - 1;
	i = 0;
	while(i <= j && isPalindrome)
			if(str[i] != str[j]) 
				isPalindrome = FALSE;
			printf("%s is a palindrome!\n", str);
			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;

Open in new window

Who is Participating?
Kent OlsenData Warehouse Architect / DBACommented:
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,


Open in new window

@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 ?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.