my question here is two fold I am new to recursion and I am not going to lie. I dont get it at all, well at least the code part. is this considered recursive? and the second question is how do I make a loop that would let the user select y to enter another palindrome or n to quit. I know the laast part is easy I have been trying a do while loop but to no avail.

#include <stdio.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

main () 
{

	char str[30];
	int isPalindrome = 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);
		}

}

Recommended Answers

All 4 Replies

First: I believe you know what functions are? Or at least you should. Well, recursion is when you call some function inside itself.
This is not a recursion!

Second: If you want to make a simple menu (like that what you said, y to enter another, n to exit) you have to encapsulate most of your code (best would be to make a function) and put it in while loop, something like this:

char decision;
int flag = TRUE;
while (flag){
        /*do some stuff here*/

        printf ("Do you want to continue (y/n)\n");
        scanf("%c", &decision);
        /*be careful, \n stays in input buffer*/
        if (decision == 'y') flag = TRUE;
        else flag = FALSE;
}

man I have been struggling with this recursion thing for days now and I have not gotten it yet, but the way that you put it was the easiest explination that I have recieved. How you would make this program a recursive one?

char decision;
int flag = TRUE;
while (flag){
        /*do some stuff here*/

        printf ("Do you want to continue (y/n)\n");
        scanf("%c", &decision);
        /*be careful, \n stays in input buffer*/
        if (decision == 'y') flag = TRUE;
        else flag = FALSE;
}

What a gross input statement. See this.

A recursive palindrome is pretty simple, here is my attempt at the pseudo-code for it.

function isPalindrome(char* str, int len):
  if len <= 1:
     return true; // we have a palindrome
  if len > 1;
     if str[0] == str[len]
       isPalindrome (str + 1, len - 1);
     else
        return false; // not a palindrome
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.