-3

When I print out text2 I see that it is definitely not the reverse of the string I gave it and I'm not sure why that is. When I put in "test" I get stuff like "ȍ\2200+". Can I use strncpy on char arrays? Maybe it needs to be done with a loop - not sure. Any help would be appreciated. :)

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    char text[79], text2[79];
    bool input = true;

    while (input) {
        cout << "Please give me a line of text to examine: ";
        cin.getline(text, 79);

        for(int i = 0; i < strlen(text); i++ )
            cout << text[i];

        // test to see if it is a palindrome
        strncpy(text, text2, 80);

        reverse(text2, text2 + strlen(text2));

        printf("%s", text2);  `// when I print this out I get something odd`

        if (strcmp(text, text2) == 0)
            cout << " is a palindrome!" << endl;
        else
            cout << " is not a palindrome." << endl;

        if (strcmp(text, "END") == 0)
            input = false;
         else
            cout << "\ntype END to exit the program" << endl;

    } // end while loop
} // end main
crashmstr
  • 28,043
  • 9
  • 61
  • 79
Caleb Lawrence
  • 131
  • 1
  • 4
  • 11
  • Characters like `ȍ` should be represented as multiple bytes and it will be broken with simple byte-by-byte reverse. – MikeCAT Feb 24 '16 at 15:51
  • 1
    It's impossible to say anything for certain without seeing the actual `reverse` function, but doing `strncpy(text, text2, 80)` will most certainly lead to *undefined behavior* in some edge cases. – Some programmer dude Feb 24 '16 at 15:54
  • It seems you're using `strncpy` in a wrong way: you probably want to copy `text` into `text2`, not the other way around. – blazs Feb 24 '16 at 15:56
  • Talking about your `strncpy` call, I think you need to [read more about it](http://en.cppreference.com/w/cpp/string/byte/strncpy). – Some programmer dude Feb 24 '16 at 15:56
  • Oh, for some reason I thought that was something c++ did. Weird, okay thanks. – Caleb Lawrence Feb 24 '16 at 15:56
  • Oh yeah I switched the order of text and text2 with the strncpy and it worked! Thanks a lot guys. :) – Caleb Lawrence Feb 24 '16 at 15:58

3 Answers3

2

It seems you're using strncpy in a wrong way: you probably want to copy text into text2, not the other way around.

There's a much simpler way to test whether a string is a palindrome, namely:

bool is_palindrome(const char* s, size_t n) {
  size_t i, j;

  i = 0, j = n-1;
  while (i < j && s[i++] == s[j--])
    ;
  return i >= j;
}
blazs
  • 4,705
  • 24
  • 38
1

Why not use std::vector<char> and std::reverse from <algorithm> to handle your problem?

I would do something like below:
(note that I'm using C++11 range-based for loop and auto which you can change to a regular for loop and use std::string line if you don't have a compiler supporting this).

int main()
{
    cout << "Please give me a line of text to examine: ";
    auto line = ""s;
    getline(cin, line);

    // Push back every character to the vector
    vector<char> vtext;
    for (const auto &elem : line)
        vtext.push_back(elem);

    // Create a copy of the vector<char> and reverse the copy
    vector<char> vtext_reversed{vtext};
    reverse(begin(vtext_reversed), end(vtext_reversed));

    // Print the line reversed
    cout << "\nThis is the line reversed: ";
    for (const auto &elem : vtext_reversed)
        cout << elem;
}
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
0

Typically you'll see this reversal technique for char*:

void reverse(char* s) {
    if(!s) return;
    size_t n = strlen(s);
    for(size_t i = 0; i < n/2; ++i) {
      char tmp = s[i];
      s[i] = s[n - i - 1];
      s[n - i - 1] = tmp;
    }
}

This will not work, however, with non-ASCII characters. The reason is that non-ASCII characters require multiple bytes to represent.

You will need to use wide characters to handle multi-byte codepoints, but the logic should follow above.

erip
  • 16,374
  • 11
  • 66
  • 121