Sixth, it appears that you are using namespace std;
somewhere in your code as you did not prefix stack
or string
. This is not a good idea because namespaces can contain functions with the same name, so you can have problems with your code when you start using multiple namespaces, or even if you have a function in the same file that has the same name as a function in the namespace. You can read more about it here here.
Sixth, it appears that you are using namespace std;
somewhere in your code as you did not prefix stack
or string
. This is not a good idea because namespaces can contain functions with the same name, so you can have problems with your code when you start using multiple namespaces, or even if you have a function in the same file that has the same name as a function in the namespace. You can read more about it here.
Sixth, it appears that you are using namespace std;
somewhere in your code as you did not prefix stack
or string
. This is not a good idea because namespaces can contain functions with the same name, so you can have problems with your code when you start using multiple namespaces, or even if you have a function in the same file that has the same name as a function in the namespace. You can read more about it here.
Sixth, it appears that you are using namespace std;
somewhere in your code as you did not prefix stack
or string
. This is not a good idea because namespaces can contain functions with the same name, so you can have problems with your code when you start using multiple namespaces, or even if you have a function in the same file that has the same name as a function in the namespace. You can read more about it here .
So, with the word "qweewq", you first remove "q" so the word is "weewq". Next, i
== 1, so you remove "e", so the word is "wewq", and so on. You should change this to:
bool isPalindrome(std::string word) {
int wordSize = word.size();
// #1 load the stack
std::stack<char> s;
for (int i = 0; i < wordSize / 2; i++) {
s.push(word[0]);
word.erase(0, 1);
}
// #2 make sure word size is same as stack size (handle odd-character palindromes)
if (word.size() > s.size()) {
word.erase(0, 1);
}
// #3 if our stack is empty, then return true
if (s.size() == 0) {
return true;
}
// #4 validate palindrome
while (s.size() > 0) {
if (s.top() == word[0]) {
s.pop();
word.erase(0, 1);
// return true only when our stack is empty
if (s.size() == 0 || word.size() == 0) {
return true;
}
}
else {
return false;
}
}
}
So, with the word "qweewq", you first remove "q" so the word is "weewq". Next, i
== 1, so you remove "e", so the word is "wewq", and so on. You should change this to:
bool isPalindrome(string word) {
int wordSize = word.size();
// #1 load the stack
stack<char> s;
for (int i = 0; i < wordSize / 2; i++) {
s.push(word[0]);
word.erase(0, 1);
}
// #2 make sure word size is same as stack size (handle odd-character palindromes)
if (word.size() > s.size()) {
word.erase(0, 1);
}
// #3 if our stack is empty, then return true
if (s.size() == 0) {
return true;
}
// #4 validate palindrome
while (s.size() > 0) {
if (s.top() == word[0]) {
s.pop();
word.erase(0, 1);
// return true only when our stack is empty
if (s.size() == 0 || word.size() == 0) {
return true;
}
}
else {
return false;
}
}
}
Sixth, it appears that you are using namespace std;
somewhere in your code as you did not prefix stack
or string
. This is not a good idea because namespaces can contain functions with the same name, so you can have problems with your code when you start using multiple namespaces, or even if you have a function in the same file that has the same name as a function in the namespace. You can read more about it here .
So, with the word "qweewq", you first remove "q" so the word is "weewq". Next, i
== 1, so you remove "e", so the word is "wewq", and so on. You should change this to:
bool isPalindrome(std::string word) {
int wordSize = word.size();
// #1 load the stack
std::stack<char> s;
for (int i = 0; i < wordSize / 2; i++) {
s.push(word[0]);
word.erase(0, 1);
}
// #2 make sure word size is same as stack size (handle odd-character palindromes)
if (word.size() > s.size()) {
word.erase(0, 1);
}
// #3 if our stack is empty, then return true
if (s.size() == 0) {
return true;
}
// #4 validate palindrome
while (s.size() > 0) {
if (s.top() == word[0]) {
s.pop();
word.erase(0, 1);
// return true only when our stack is empty
if (s.size() == 0 || word.size() == 0) {
return true;
}
}
else {
return false;
}
}
}