Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* Sachie Holtz CA110 pl5.cpp
programming Lab 5: Palindrome Program
To examine a user-entered string to determine if it's a palindrome.
*/
#include <iostream>
#include <cstring>
using namespace std;
bool is_palindrome( char inputString[] );
int main()
{
char userInput[20];
cout << "please enter a word:";
cin.getline( userInput, 20 );
if ( is_palindrome( userInput ) )
{
cout << "The word you entered: " << userInput << " is a palindrome" << endl;
} else {
cout << "The word you entered: " << userInput << " is not a palindrome" << endl;
}
return 0;
}
bool is_palindrome( char inputString[] )
{
int len = strlen( inputString );
char reversed[20];
// reverse the string
for (int index=0; index<len; index++)
{
char c = inputString[ len-index-1 ];
reversed[ index ] = c;
}
// compare strings
bool stringsMatch = true;
for (int index=0; index<len; index++)
{
if (inputString[ index ] != reversed[ index ]) {
stringsMatch = false;
}
}
return stringsMatch;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.