This assignment asks you to write a program that checks to see if a line of input is a Palindrome. A Palindrome is a word or phrase that is the same when read forward or backward.
bool Palindrome (string sText)
that tests a string to see if it is a palindrome.
getline()
cin
is delimited by white space. This makes it impossible to get more than one word in an string
using cin
. Instead, use
getline
.
For example, the following will get a line of input from the standard input device and store it in sString
:
getline(cin, sString);
string
can be accessed with an index, a number in square brackets. Like most things in C++, strings are zero-based, meaning the first character is at position zero.
string sString = "hello";
char cChar = sString[0];
//stores the letter h in cChar
length()
length()
function will return the total number of characters (including spaces) in the string. For example:
string sString = "Kenny Roberts Jr.";
cout<<sString.length();
//displays 17
bool Palindrome (string sText)
true
otherwise it should return false
.
You can use the function as part of an if statement:
if(Palindrome(sString))
cout<<sString<<" is a Palindrome";
#include <ctype.h>
ctype.h
header file declares useful functions for determining the type of the character. The functions isalpha() and isdigit()
will tell you if a character is a letter or a number. For this assignment you will find isalnum() which returns true if a character is a letter or a digit, and toupper()
or tolower() which return the corresponding upper and lower case for letters (and leaves other characters unchanged) helpful.