0

I'm new to programming. I've learned how to reverse a string today. I've tried to use string instead of char but terminal gives an error.

string name = { "george" };
int nChar = sizeof(name) - 1;
string *pName = &name;
string *pNameLast = &name + nChar - 1;

while(pName < pNameLast) {
    string save = *pName;
    *pName = *pNameLast;
    *pNameLast = save;
    
    pName++;
    pNameLast--;
}

cout << name << endl;

enter image description here

  • 1
    please include the error in the question. Note that there is an algorithm `std::reverse`, I guess you want to go for the handwritten, right? – 463035818_is_not_an_ai Dec 18 '20 at 13:00
  • 1
    What do you intend to achieve with `string *pNameLast = &name + nChar - 1;`? Do you know what that does? It seems like you just replaced every instance of `char` with `string`. – Lukas-T Dec 18 '20 at 13:00
  • 1
    Hint - you cannot just replace `char*` with `std::string` and expect your code to work. – Yksisarvinen Dec 18 '20 at 13:02
  • i understand. but how can i reverse it using a string? i don't really understand how it works :\ –  Dec 18 '20 at 13:03
  • `sizeof(name)` will give you the size of the `std::string` object, which is *not* the same as the length of the string it wraps. – Some programmer dude Dec 18 '20 at 13:04
  • Check the value on `nChar`. Besides, instead of trying to use pointers, simply use `name[i]`... – Damien Dec 18 '20 at 13:05
  • 3
    I suggest you invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn C++ properly. Perhaps even take a few classes. – Some programmer dude Dec 18 '20 at 13:05
  • In order to get to the pointer of the beginning of the array contained in string, `&name` should be replaced by `reinterpret_cast(name.data())` with c++11 (there is a 2 bytes difference). – Soleil Dec 18 '20 at 13:13
  • @Soleil-MathieuPrévot there shouldnt be any `&name` in the code in the first place – 463035818_is_not_an_ai Dec 18 '20 at 13:14
  • 1
    Does this answer your question? [How do you reverse a string in place in C or C++?](https://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c) – JHBonarius Dec 18 '20 at 13:59

3 Answers3

0

Here is an example of using std::reverse_interator. This is better a better way of doing it. Rather than doing it yourself.

#include <iostream>
#include <string>
#include <iterator>
 
int main()
{
    std::string s = "George";
    std::reverse_iterator<std::string::iterator> r = s.rbegin();
    std::string rev(r, s.rend());
    std::cout << rev << endl;
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
Bybit360
  • 166
  • 3
  • 11
  • 1
    You probably want to use `rbegin` and `rend`, since right now you're dereferencing `end`. – Stephen Newell Dec 18 '20 at 13:47
  • Stephen Newell is right, it is giving me an extra space at the start that I suppose is from the [Undefined Behavior caused by dereferencing `std::string::end()`](https://stackoverflow.com/questions/61669762/can-i-dereference-stdstring-end). – Gary Strivin' Dec 18 '20 at 14:08
  • @GaryNLOL You are right. Let me fix that. – Bybit360 Dec 18 '20 at 14:21
0

If you want to reverse a string object, just do the following (note this is the one of many handmade ways:

#include <iostream>
#include <string>

int main() {

    std::string my_string{"hello"};

    // Similar to your way:
    int index{static_cast<int>(my_string.length() - 1)};
    for (; index >= 0; --index) {
        std::cout << my_string.at(index) << std::endl;
    }
}
Krapnix
  • 257
  • 2
  • 8
0

Problem:

When you attempt to do operations like string *pName = &name and string *pNameLast = &name + nChar - 1;, you're treating std::string's as char*'s. You cannot do that and expect the code to work.

Solution:

Treat std::string's as std::string's. Instead of doing string *pName = &name declare pName as an int and use the operator [] of std::string to access to the values. Similarly with pNameLast. You also have to declare string save as a char or, as I prefer, use the std::swap function.

Additional info:

  1. As far as I can see you're probably using using namespace std; as you're declaring std::string as string. If that is the case consider that using namespace std; is considered a bad practice (More info here).

Full code:

#include <iostream>

int main() {
    std::string name = "george";
    int length = name.size();
    int pNameFirst = 0;
    int pNameLast = length - 1;

    while(pNameFirst < pNameLast)
    {
        std::swap(name[pNameFirst],name[pNameLast]);

        pNameFirst++;
        pNameLast--;
    }
    std::cout << name << std::endl;
}
Gary Strivin'
  • 908
  • 7
  • 20