recursive problem

Posted on 2008-10-06
Last Modified: 2010-04-15
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

Question by:mikeregas

Expert Comment

ID: 22654566


Open in new window

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 ?
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,

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now