Palindrome Recursive

Make a program that will determine if the entered string is a palindrome or not using the recursion. MAke sure it accepts space and uppercase letter
ex:
enter a string: raceacar
its a palindrome

enter a string: raceAcar
its a palindrome

enter a string: race Acar
its a palindrome


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 #include<iostream>
#include<string>
#include<cctype>
using namespace std;

int palindrome(char str[], int length)
{
	if (*str == str[length - 1])
	{
		palindrome(str + 1, length - 1);
	}
	else
	{
		return false;
	}
	return true;
}

int main()
{
	char p[100];
	cout << "Enter a string: ";
	cin.getline(p, 100);

	if (isspace(p[100]))
		return true;
	else
		return false;
	if (isupper(p[100]))
		return true;
	else
		return false;

	if (palindrome(p, 100) == true)
		cout << "It's a palindrome.\n";
	else
		cout << "It's not a palindrome\n";

	system("pause>0");
	return 0;
}
Your palindrome algorithm works fine, the issue is in main. It's never making it to
1
2
3
4
	if (palindrome(p, 100) == true)
		cout << "It's a palindrome.\n";
	else
		cout << "It's not a palindrome\n";


because of the 4 returns above it.

1. Write functions that remove spaces and change upper case to lower case, don't return true or false from main.

2. Think about how to find the actual length of the word(Hint: it's not 100 and it can be found with a loop)
Last edited on
Topic archived. No new replies allowed.