Skip to content

Instantly share code, notes, and snippets.

@kumavis
Created May 4, 2014 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumavis/f7e5e10c118aceb09921 to your computer and use it in GitHub Desktop.
Save kumavis/f7e5e10c118aceb09921 to your computer and use it in GitHub Desktop.
/* 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