-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.cpp
60 lines (56 loc) · 1.9 KB
/
palindrome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// first take out all the spaces and punctuation from the sentence
// do I also need to convert caps to lowercase?
// compute midpoint
// check to make sure letters that are equidistant from the midpoint are the same
// do this slightly differently depending on whether the number of characters is even or odd
#include <string>
class Solution {
public:
bool isPalindrome(string s) {
string onlyLetters;
onlyLetters = rem_stuff(s);
int l = onlyLetters.length();
if(l == 1){
return true;
}
int low_indx, high_indx;
if (l%2 == 0){
high_indx = l/2;
low_indx = high_indx - 1;
}
else{
low_indx = l/2 - 1;
high_indx = l/2 + 1;
}
// at this point all phrases are at least 2 characters long
do{
if(onlyLetters[low_indx]==onlyLetters[high_indx]){
--low_indx;
++high_indx;
}
else{
return false;
}
}while(low_indx>=0);
return true;
}
string rem_stuff(string s){
int stl = s.length(); //compute length of incoming string only once;
string f = s; // set it equal to s then take out spaces and punctuation
int findx = 0;
for(int i=0; i<s.length(); i++){
if(ispunct(s[i])||isdigit(s[i])||isspace(s[i])){
//if it's any of those things remove it and continue to next
// actually, should keep the digit option?
f.erase(findx, 1);
// don't update findx becasue now that index contains the next value
}
else{
f[findx]=tolower(f[findx]); // can I do this if digits are an option?
++findx;
//update findx every time a character is one I want to keep
}
}
return f;
}
};