recursive problem

Posted on 2008-10-06
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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 46

Accepted Solution

Kent Olsen earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
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 how to create, access, and change arrays in the C programming language.

718 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