-
Notifications
You must be signed in to change notification settings - Fork 2
/
palindrome_two.cpp
66 lines (56 loc) · 1.99 KB
/
palindrome_two.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
61
62
63
64
65
66
/*
Palindrome Two
#search #string manipulation
Have the function PalindromeTwo(str) take the str parameter being passed and return
the string true if the parameter is a palindrome, (the string is the same forward
as it is backward) otherwise return the string false. The parameter entered may have
punctuation and symbols but they should not affect whether the string is in fact
a palindrome. For example: "Anne, I vote more cars race Rome-to-Vienna" should return
true.
Optimal: o(n), achieved: o(n)
*/
#include <iostream>
#include <string>
#include <cctype>
bool isalpha_m(char c) {
return static_cast<bool>(std::isalpha(static_cast<unsigned char>(c)));
}
/* omits non alphanumerics */
std::string PalindromeTwo(std::string str) {
//code goes here
// two indexes running from left to right and right to left
size_t i{ 0 };
size_t j{ str.length() - 1 };
bool foundNextAlphanum{true};
// while both indexes are in bounds
while ( (j >= 0 && i <= str.length() - 1) && (i >= 0 && j <= str.length() - 1) ) {
// until both indexes are alphanumeric
while ( !isalpha_m(str.at(i)) && i < str.length() - 1) {
i++;
if( i == str.length() - 1 ) { foundNextAlphanum = false; }
}
while ( !isalpha_m(str.at(j)) && j > 0 ) {
j--;
if( j == 0 ) { foundNextAlphanum = false; }
}
// check corresponding alphanumerical characters
// if foundNextAlphanum is false, that means that one of the indexes
// when increasing or decreasing (in case of i or j accordingly) did not
// found the next alphanumerical value to compare with and reached the end
// of the string
if (foundNextAlphanum && tolower(str.at(i)) != tolower(str.at(j))) {
return "false";
}
i++;
j--;
}
return "true";
}
#ifndef CODERBYTE_CHALLENGES_TEST_CPP_FLAG
int main(void) {
// keep this function call here
// std::cout << PalindromeTwo(coderbyteInternalStdinFunction(stdin));
std::cout << PalindromeTwo("!Al!lets!Della!call!Ed!\"Stella\".");
return 0;
}
#endif