-3

First of all, I very much appreciate any help you are willing to provide. I am new to C++ and have been scouring this website as well as other resources for the solution to my problem.

Further, this was indeed a portion of a homework assignment. However, the assignment has been turned in (upsettingly, without getting this code to work). It would be great to get an explanation for what the problem in my specific code is and how to fix my current code, rather than the just rewritten code with a different way to approach to problem. I certainly found plenty of ways to solve this problem on this wonderful site!

I am getting no errors with my code, however the reversal output is not showing the reversed character array. This results in my little program here always showing "Your string is not a palindrome! :(" no matter what the input is.

#include <iostream>
#include <string>

using namespace std;

int isPalindrome(char *input, char *input2);
char reverseString(char *input);

int main ()
{
    char input[50];
    char input2[50];

    cout << "Please enter a string of characters no larger than 50." << endl;
    cin.getline(input, 50);
    reverseString(input);
    cout << "The reversed string is " << input2 << endl;
    int result;
    result  = isPalindrome(input, input2);

    if(result == 0)
            cout << "Your string is a palindrome!" << endl;
    else
            cout << "Your string is not a palindrome! :( " << endl;
return 0;
}

int isPalindrome(char* first, char* second)
{
    if (*first == *second)
            return 0;
    else
            return 1;
}

char reverseString(char* input2)
{
    int size = sizeof(input2);
    for (int i = 0; i < (size/2); i ++)
            swap(input2[i], input2[size-i-1]);

return *input2;
}

Again, I appreciate any help you can provide! I apologize if this is a simple error that I am overlooking and should have been able to find elsewhere.

K.Estes
  • 1
  • 2
  • 1
    What did you observe when stepping through your code with the debugger? – user0042 Jul 20 '17 at 06:03
  • With `*first == *second` you compare the first character of `first` with the first character of `second`. That expression is equal to `first[0] == second[0]`. – Some programmer dude Jul 20 '17 at 06:04
  • 2
    Furthermore, doing `sizeof` on a pointer gives you the size of *the pointer*, not what it points to. A quick step-through in a debugger would have told you that very quickly. Please learn how to use a debugger to step through your code line by line to find out things like this. – Some programmer dude Jul 20 '17 at 06:06
  • Your compiler should have warned you that you didn't initialise `input2`, and if it didn't, please read the manual for it to see how to get it to do so. – Ken Y-N Jul 20 '17 at 06:07
  • Or perhaps you should start by taking a few stack back from what you're doing, [find a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over from the beginning. Because you make a few very basic mistakes that indicates you don't really know what you're doing. Something a good book or two hopefully should help with. – Some programmer dude Jul 20 '17 at 06:10
  • @Someprogrammerdude Thank you I will certainly learn how to use a debugger. We are currently using Putty and vi so I am unsure how to do this. I will try to open my code in visual studio and use their debugger. Also, yes I will attempt to read through my book again. Intro to C++ is certainly a difficult class! I appreciate the help. – K.Estes Jul 20 '17 at 21:46
  • @KenY-N I was under the assumption that my line of code `char input2[50];` inside my main function did this. Since it is not global, the array is initialized to NULL. Is this incorrect? – K.Estes Jul 20 '17 at 21:47

1 Answers1

0

Checking for a palindrome does not take this much effort.

bool isPalindrome(const char* s)  // this function is self-contained.
{                                 // the caller does not need to provide
    size_t n = strlen(s);         // any pre-computed value.
    if (n == 0)
        return false;

    const char* e = s + n - 1;
    while (s < e)
        if (*s++ != *e--)
            return false;
    return true;
}

int main ()
{
    char input[50];

    cout << "Please enter a string of characters no larger than 50." << endl;
    cin.getline(input, 50);

    bool result = isPalindrome(input);

    cout << "Your string is" 
         << ((result) ? " " : " not ") 
         << "a palindrome!\n";

    return (result) ? 1 : 0;
}

In your reverseString function:

char reverseString(char* input2)
{
    int size = sizeof(input2);                 // <-- ?? sizeof(char*) != strlen(input2)
    size_t size = strlen(input2);              // <-- should read.

    for (int i = 0; i < (size/2); i ++)
            swap(input2[i], input2[size-i-1]);

    return *input2;                            // what's this? returning first char? why?
}
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • Thank you for the help. I will work through the code you provided and attempt to better understand it. I see now why passing the pointer is not working. Late last night....not so much! I still need to determine why it was giving me an error every time i tried to pass `char input2` to the function and insisiting that I passed `char* input2` instead. – K.Estes Jul 20 '17 at 21:50
  • It's pretty simple. Walk the string from tboth ends, compare and exit when there is either a difference - not a palindrome - or the two pointers cross - a palindrome. Your IsPalindrome function only compares the first and last bytes. – Michaël Roy Jul 20 '17 at 22:07