I need this to been done recursively, it works but I was told that it is not recursive does anyone have any ideas or suggestions

#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;
	}
}

Recommended Answers

All 3 Replies

I found a recursive code in c++, but I do need it in C, if there is anyone out there that is willing to convert it that would be great. thanks

#include <iostream>  
      
    using namespace std;  
      
    bool recursivePalindrome(std::string str, unsigned int index = 0) {  
        if (index > str.length()/2) return true;  
        if (str[index] == str[str.length()-index-1])  
            return recursivePalindrome(str, ++index);  
        else return false;  
   }  
     
   int main () {  
       cout << recursivePalindrome("yehey");  
       cout << recursivePalindrome("w00t");  
     
       cin.get();  
       return EXIT_SUCCESS;  
   }

Something like this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int recursivePalindrome(char *str, unsigned int index)
{
    if (index > strlen(str)/2) return 1;
    if (str[index] == str[strlen(str)-index-1])
        return recursivePalindrome(str, ++index);
    else return 0;
}
int main ()
{
    printf("%d\n",recursivePalindrome("yehey",0));
    printf("%d\n",recursivePalindrome("w00t",0));
    return EXIT_SUCCESS;
}

Oh, it's much more absurd example of a recursive method than famous recursive factorial!
It's rather rough C++ to C translation. Slightly better one:

int recursivePalindrome(const char *str, unsigned int index) {
    unsigned length = (str?strlen(str):0);
    ... and so with obvious code correction

... and so on with obvious code correction. The matter is that str.length() is a simple property getter. Don't add once more nonsense - strlen call in a for loop condition...

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.