Created
February 6, 2014 19:43
-
-
Save NeilHanlon/8851209 to your computer and use it in GitHub Desktop.
This file contains 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
#include <iostream> | |
#include <string> | |
#include <cctype> | |
#include "Helpers.h" | |
using namespace std; | |
bool is_pal(const string& s); | |
string remove_punct(const string& s, const string& punct); | |
string reverse(const string& s); | |
void swap(char& v1, char& v2); | |
int main(){ | |
string input; | |
cout << "Enter a string: "; | |
getline(cin,input); | |
if(is_pal(input)) | |
cout << "\"" << input + "\" is a palindrome."; | |
else | |
cout << "\"" << input + "\" is not a palindrome."; | |
return 0; | |
} | |
bool is_pal(const string& s){ | |
string punct(",;:.'?\"!"); | |
string str(s); | |
str = Helpers::strToLower(str); | |
string lower_str = remove_punct(str,punct); | |
return (lower_str == reverse(lower_str)); | |
} | |
string remove_punct(const string& s, const string& punct){ | |
string no_punct; | |
int strlen = s.length(), | |
punctlen = punct.length(); | |
for(int i = 0; i< strlen; ++i){ | |
string a = s.substr(i,1); | |
int loc = punct.find(a,0); | |
if(loc < 0 || loc >= punctlen){ | |
no_punct = no_punct + a; | |
} | |
} | |
return no_punct; | |
} | |
void swap(char& v1, char& v2){ | |
char temp = v1; | |
v1 = v2; | |
v2 = temp; | |
} | |
string reverse(const string& s){ | |
string temp = s; | |
int start = 0, | |
end = s.length(); | |
while (start < end){ | |
end--; | |
swap(temp[start], temp[end]); | |
start++; | |
} | |
return temp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment