1

I"m creating a function which returns true or false on whether a word is a palindrome, which also disregards spaces in the string.

For example "taco cat" would be considered a palindrome.

I'm taking in the string to be checked through getline, however wordA.length is always returning 0, and I'm not sure what to fix.

Any help would be appreciated.

Thanks!

bool checkPalindrome() {
string wordA;
cout << "The word is: " << endl;
getline(cin, wordA);
cin.clear();
cin.ignore(10000, '\n');

//This outputs 0
cout << wordA.size() << endl;

char* wordB = new char[wordA.length()+1];
int j = 0;

for (unsigned int i = wordA.length() - 1; i > 0; i--) {
    wordB[j] = wordA[i];
    j++;
}

int k = 0;
for (unsigned int i = 0; i < wordA.length(); i++) {
    if (wordA[i] == ' ') {
        continue;
    }
    while (wordB[k] == ' ') {
        k++;
    }
    if (wordA[i] != wordB[k]) {
        cout << "It's not a palindrome" << endl;
        delete[] wordB;
        return false;
    }
    k++;
}

cout << "It is a palindrome" << endl;
delete[] wordB;
return true;
}

int main()
{
int response;
cout << "Enter test case "        << endl << endl;
cout << "0: Sort"                         << endl;
cout << "1: Get Permutations"             << endl;
cout << "2: Check Permutations"           << endl;
cout << "3: Check Permutation Palindrome" << endl << endl;
cout << "Selection: ";

cin >> response;

switch (response) {
case 0:
    mergeCase();
    break;
case 1:
    permutationCase();
    break;
case 2:
    checkPermutation();
    break;
case 3:
    checkPalindrome();
    break;
}
}
000
  • 99
  • 1
  • 7
  • 1
    Is there a specific reason you use pointers and dynamic allocation using `new[]` for `wordB`? Why don't you use `std::string` for that variable too? – Some programmer dude Aug 31 '17 at 05:29
  • By the way, your copy-loop will not copy the first character of `wordA`. And there's a multi-line comment-ending after it that doesn't have a matching start. – Some programmer dude Aug 31 '17 at 05:30
  • Then for your problem... I'm ***guessing*** (have to guess since you don't show a [Minimal, **Complete**, and Verifiable Example](http://stackoverflow.com/help/mcve)) that you read some input before you call this function? Some input which leaves a newline in the input buffer. – Some programmer dude Aug 31 '17 at 05:32
  • Use length instead of size – Artemy Vysotsky Aug 31 '17 at 05:38
  • @ArtemyVysotsky For [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) the [`length` and `size`](http://en.cppreference.com/w/cpp/string/basic_string/size) functions are equal. – Some programmer dude Aug 31 '17 at 05:42
  • @Someprogrammerdude I thought I had to use dynamic allocation since I'm getting the string wordA from the user, and since the length of wordB is dependent on wordA, that would call for dynamic allocation. Am I mistaken? I added my int main(), and I have the user input an integer to make a selection. Also why won't my copy loop copy the first character, and how can I make it so that it does? – 000 Aug 31 '17 at 05:46
  • Read e.g. [this `std::string` constructor reference](http://en.cppreference.com/w/cpp/string/basic_string/basic_string) and you should find a way to pre-allocate a string of a certain length. For the loop, think about what the index of the first character is. Will the loop ever reach that index? (If unsure, use a debugger to step through the code). And lastly, if you read input using e.g. `std::cin >> some_variable;` then the ending newline will be left in the buffer, for the next input operation (for example `std::getline`) to read. – Some programmer dude Aug 31 '17 at 05:51
  • @Someprogrammerdude I'm not sure I understand what you mean regarding the index of the first character. When I place my breakpoints where I'm dynamically allocating memory for wordB I get the error "0xccccccccc ." Regarding the for loop, isn't the index j for wordB initialized to 0, and the index for wordA initialized to wordA.length() - 1? I don't see where the problem is. – 000 Aug 31 '17 at 06:08
  • You never reach `0` in the loop. The loop ends when `i == 0`, and you will never copy the character at `wordA[0]`. Change the condition to `i >= 0`. – Some programmer dude Aug 31 '17 at 06:11
  • The debugger "error" is probably because you stop *before* the actual allocation happens. If you step over it you should get a valid pointer. However, the reason I asked you to step through the code in a debugger is because of the loop. – Some programmer dude Aug 31 '17 at 06:12
  • Talking about the loop, what do you think happens when `wordA` is empty (as it happens for you, because of the problem discussed in my previous comments) and you do `wordA.length() - 1`? What will the initial value of `i` be? – Some programmer dude Aug 31 '17 at 06:14

1 Answers1

0

Possibly this after your menu code. Can't reproduce as I don't have all the code required, nor the inputs you have tested.

cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Josh
  • 12,262
  • 2
  • 38
  • 46