palindrome need help

Hello everyone,
I am very new to C++ and have no programming experience. I need help with palindrome. My professors says its not correct that, I need to use functions, use string class instead of array of chars, headers are missing, and END is evaluated as a word when it should not be. I am lost and do not where to start making corrections.




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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <ctype.h>

using namespace std;

int main()
{
char word972[83];
do{
cout << "Please enter a word to find out if it is a palindrome or type END to terminate." << endl;
cout << endl;
cin>>word972;
int length = strlen(word972);

// Bool and for loop for word input.
bool palindrome=true;
for (int i=0; i<length; i++)
{
if(toupper(word972[i])!=toupper(word972[length-i-1]))
if(word972[i]!=word972[length-1-i]) 
palindrome=false;
}

// Output of palindrome respone.
if(palindrome==true) cout << word972 << " is a palindrome." << endl;
cout << endl;
if(palindrome==false) cout << word972 << " is not a palindrome." << endl;
cout << endl;

// Whlie loop until END it typed to terminate.
}while (0 != strcmp(word972, "END"));

return(0);
}
Last edited on
1.) You could split up the code in two, possibly three functions:

a.) the main function
b.) a function which formats the string (removes special characters or changes capitals to lower-case).
c.) a function which returns a bool, and takes a string. It returns true if the string is a palindrome, and false if it isn't

2.) The std::string class is terribly easy to work with. It's definitely an improvement from char arrays. I'll write an example for your case using strings shortly.

3.) Which headers are missing, according to your instructor?

4.) When you're dealing with std::strings, you won't be using strcmp to begin with.


Here's an example. I've left out the actual meat of the program for you to figure out:

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
#include <iostream>
#include <string>

void lowerString(const std::string& string) {
	/**/
}

bool isPalindrome(const std::string& string) {
	/**/
}

int main() {
	std::string string;
	bool loop = true;
	while (loop) {
		std::cout << "Enter a string (\"END\" to end):\t";
		std::cin >> string;

		loop = (string != "END");
		if (!loop) { break; }


		lowerString(string);//makes capitals lowercase
		if (isPalindrome(string)) {
			std::cout << string << " is a palindrome" << std::endl;
		}
		else {
			std::cout << string << "is not a palindrome" << std::endl;
		}

		std::cin.ignore();
		std::cin.get();
		system("cls");
	}
	return 0;
}
Last edited on
The instructor did not state which headers are missing. I will continue to work at the program. It just that I find it hard to understand. Thank you for your help.
Last edited on
Topic archived. No new replies allowed.