Solved

recursive problem

Posted on 2008-10-06
4
250 Views
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: ");
	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;
	}
}

Open in new window

0
Comment
Question by:mikeregas
4 Comments
 
LVL 9

Expert Comment

by:mgonullu
ID: 22654566

<Deleted>

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
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

by:
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why adding test code changes the build ? 11 132
why "." vs "->" 23 120
sameEnds challenge 3 156
SBS server C drive at zero no matter what I delete . Where is the disk space going ?? 9 80
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-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.

813 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

12 Experts available now in Live!

Get 1:1 Help Now