0

I am trying to create a program that reverses a string with pointers and functions in C++, but so far, my method is only returning an empty string rather than a reversed one.

#include <iostream>

using namespace std;

void invertS(string *str_ptr );

int main()
{
   string value {"Hello"};
   string *str_ptr {nullptr};
   str_ptr = &value;
   invertS(str_ptr);
   cout << value;
};

void invertS(string *str_ptr) {
    string str = *str_ptr;
    string temp = "";
    int length = str.length();
    int j = length - 1;
    for (int i = 0; i < length; i++) {
        temp[i] = str[j];
        j--;
    };
    *str_ptr = temp;
};

The result is always just an empty string. Note that I'm assigning the string referenced by str_ptr to a new string inside the function so that I can use .length on the string. Since .length cannot be used on a pointer as far as I know. If there's a better way to do that, I'd also appreciate to know how it goes.

Dasphillipbrau
  • 524
  • 2
  • 8
  • 17
  • Think about the *size* of the string `temp`... Do you ever make it bigger? – Some programmer dude Oct 24 '19 at 06:48
  • _Since .length cannot be used on a pointer as far as I know._ You could do `str_ptr->length()` instead. – Scheff's Cat Oct 24 '19 at 06:48
  • Just use [`std::reverse(s.begin(), s.end());`](https://en.cppreference.com/w/cpp/algorithm/reverse) where s is the `std::string`. – nada Oct 24 '19 at 06:51
  • 1
    Possible duplicate of [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) – nada Oct 24 '19 at 06:56
  • `std::string newstring{oldstring.rbegin(), oldstring.rend()};` if you want a reversed copy instead of reversing it in-place. – Shawn Oct 24 '19 at 07:12
  • No offense but that question you're linking to has a ridiculously verbose solution whereas the one provided here by Yasir Khan is much simpler. – Dasphillipbrau Oct 24 '19 at 07:24

3 Answers3

2

The problem is that you are initializing the temp string with zero length and then trying to access the indexes (which should fail IMO). The easy fix is to create a copy of the original string so that length is the same as the original string to be reversed.

// Example program
#include <iostream>

using namespace std;

void invertS(string *str_ptr );

int main()
{
   string value {"Hello"};
   string *str_ptr {nullptr};
   str_ptr = &value;
   invertS(str_ptr);
   cout << "Reversed string: " << value << endl;
};

void invertS(string *str_ptr) {
    string str = *str_ptr;
    string temp {str};
    int length = str.length();

    int j = length - 1;
    for (int i = 0; i < length; i++) {
        temp[i] = str[j];
        j--;
    };
    *str_ptr = temp;
};
Yasir Khan
  • 645
  • 4
  • 9
1

You make a new string of length 0 here:

string temp = "";

And then later you do this:

temp[i] = str[j];

However, the size of this string is still 0, so this is incorrect. You have to resize your string after making it:

temp.resize(str.length());
Blaze
  • 16,736
  • 2
  • 25
  • 44
1

I assume it's just for sake of education/learning, but you should avoid using pointers when we have so much rich C++ library.

To fix the above code, you need to append characters to the temp. Size temp has size zero. Or resize it with length

temp += str[j];

P0W
  • 46,614
  • 9
  • 72
  • 119