Created
May 4, 2014 21:56
-
-
Save kumavis/f7e5e10c118aceb09921 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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