Hey there,

So I've looked everywhere for help with my assignment until finally I found this useful service. I'm a C++ beginner and so far the problem solving and coding have fulfilled all expectations. If there is anyone to offer suggestions for my assignment, that'd be amazing!

What it is is a C++ program identifying valid palindromes of 50 characters maximum. My professor wants us to read in a string or array from the user, containing letters and/or numbers but none of the other printable characters. Also, if the user enters more than one word separated by spaces, the spaces, along with non acceptable characters, must be removed from the string/array and copied into a parralel array or string.

I've started working but haven't made much progress. My approach to the problem has been to read the user's input from function main(), validate the input of 50 characters (while loop), promote characters to uppercase and copy the input into another string/array. I then plan on passing the string/array as arguments to another funtion (bool) to test the validity of the palindrome.

If there's anyone willing to tackle this problem; all help will be greatly appreciated!

Here's what I have so far:

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;

//Prototype
bool validPal(string, int);		//Test for palindrome

int main()
{
	   
	string phrase, phrase2;		//To hold string entered by user & duplicate string
	int length;					//Holds length of string characters
			

	cout << "\t\t\tIS IT A PALINDROME?\n";
	cout << "\t\t\t___________________\n";
	cout << "\n\nEnter a sentence, phrase or word no longer than\n50 characters"
		 << " to determine if it is a Palindrome:\n\n";
	
	getline(cin, phrase);		//Input from user to be tested
	
	//Validate input to 50 characters maximum
	while (phrase.length() > 50)
	{
		cout << "\nYou have entered too many characters. Please try again:\n\n";
		getline(cin, phrase);
	}

	//Assign length of the string to 'length'
	length = phrase.length();
	
	//Loop copies original string into a 2nd string
	for (int i = 0; i == phrase.length(); i++)
	{
		if (isalpha(phrase[i]))				//Test characters for letters
			phrase2[i] = toupper(phrase[i]);	//Promote letters to uppercase
	}
	
	//Displays the 2nd string
	for (int j = 0; j < phrase.length(); j++)
	{
		cout << phrase2[j];
	}
	cout << endl;
	
	
	return 0;
}

//validPal returns true/false for palindrome testing
bool validPal(string PHRASE, int LENGTH)
{
	

}

Some comments that could help you with this and future projects:

1. It's better to declare variables as soon as they are needed (and not at the beginning of the function). That way, you don't have to scroll up to check what type the variable is and you don't have to comment on the purpose of a variable as it'll be obvious.

2. It is better to pass larger objects like strings as a constant reference to a function. If you don't do this, the string will be copied, which costs time. In this program it doesn't really matter, but it's good to use references consequently where they make sense.

3. You don't need to pass the string length to the validPal function, because a string knows its length.

4. Using const for variables that won't change is good practice (i.e. const int length=...) as this signals to the reader that this variable won't change and you protect it against accidental changes (such as if (length=10)).

5. The middle part of a for loop is the condition under which the loop body continues to execute. The condition i == phrase.length() will never be true, though. This should be i < phrase.length().

6. You were trying to access elements of phrase2 even though the string had the length 0.

7. No need to print each character of a string manually - you can simply write cout << phrase2;

8. I used reverse iterators in validPal because it's easier - that requires you to make yourself familiar with the concept of iterators, though. If you don't want to deal with that yet, you probably should do the check "manually".

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

//Prototype
bool validPal(const string&);		//Test for palindrome

int main()
{
	cout << "\t\t\tIS IT A PALINDROME?\n";
	cout << "\t\t\t___________________\n";
	cout << "\n\nEnter a sentence, phrase or word no longer than\n50 characters"
		 << " to determine if it is a Palindrome:\n\n";

	string phrase;
	getline(cin, phrase);		//Input from user to be tested

	//Validate input to 50 characters maximum
	while (phrase.length() > 50)
	{
		cout << "\nYou have entered too many characters. Please try again:\n\n";
		getline(cin, phrase);
	}

	//Loop copies original string into a 2nd string
	string phrase2;
	for (unsigned int i = 0; i < phrase.length(); i++)
	{
		if (isalnum(phrase[i]))				//Test characters for letters and digits
			phrase2 += toupper(phrase[i]);	//Promote letters to uppercase
	}

	//Displays the 2nd string
	cout << phrase2 << endl;

	if (validPal(phrase2))cout << "The phrase is a palindrome!" << endl;
	else cout << "The phrase is not a palindrome." << endl;

	return 0;
}

//validPal returns true/false for palindrome testing
bool validPal(const string& phrase)
{
  return phrase==string(phrase.rbegin(),phrase.rend());
}

Thanks for the tip on reverse iterators. I will be reading up on that as I went with the manual check instead; we haven't covered that in class.

Other than that the program runs successfully!

THANK YOU FOR YOUR HELP!!!

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.