0

I'm trying to reverse a string in my C++ code line below revStr.at(j) = str.at(size);

But it doesn't change any of the elements in revStr.

Is there another way to do it without using any libraries.

#include <iostream>
#include<sstream>
#include <iterator>
using namespace std;


int main() {

    ostringstream d;

    long long c = 123456789;
    d << c;
    //cout << c << endl;
    string str = d.str();
    //cout << str.at(0) << endl;
    int size = str.size() - 1;
    //cout << size << endl;
    ostringstream e;
    e << str;
    string revStr = e.str();

for (int i = size; size==0; size--) {
    //cout << str.at(size);

        int j = 0;
        revStr.at(j) = str.at(size);
        j++;

} // End For

cout << "Original String is  :" << str << endl;
cout << "Reversed String is  :" << revStr << endl;

}
Dale K
  • 25,246
  • 15
  • 42
  • 71
Dana Amin
  • 93
  • 1
  • 1
  • 9
  • Possible duplicate of [Reverse String C++ using char array](https://stackoverflow.com/questions/24372480/reverse-string-c-using-char-array) – Jared Burrows Sep 15 '17 at 22:45
  • Why do you want to do this without any library? You do realise that `ostringstream`, `string`, and `cout` are all in the standard C++ library, so not using any library would mean you rely on non-standard mechanisms of output. With the standard library `std::reverse(revStr.begin(), revStr.end())` meets the requirement in one simple line. – Peter Sep 16 '17 at 00:24

4 Answers4

7

Use std::reverse:

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    std::string test{"Hello"};
    std::cout << "Original string: " << test << std::endl;
    std::reverse(test.begin(), test.end());
    std::cout << "Reversed string: " << test << std::endl;
    return 0;
}

Output:

Original string: Hello
Reversed string: olleH
Tyler Lewis
  • 881
  • 6
  • 19
  • I don't want to use an embedded function to reverse – Dana Amin Sep 15 '17 at 22:49
  • @DanaAmin What do you mean by “embedded function”? What’s the sense in which `std::reverse` would be embedded in `main`, but `std::string::at` isn’t? – Daniel H Sep 15 '17 at 22:52
  • I'm practicing programming man. Its for practice and I want to write a program that reverses a number. – Dana Amin Sep 15 '17 at 22:55
5

If you just want to reverse a string, you should use std::reverse, as described by Tyler Lewis. It is the best option.


If you want to learn C++, then writing your own version is good practice.

The line

for (int i = size; size==0; size--)

means “Create a new int called i and set it to size initially. Then, while size is zero, do the following and then decrement size”.

There are three problems with this:

  1. Size is not zero unless you entered a one-character string
  2. Since you never use i, there’s no point in declaring it
  3. Inside the loop you use j which is set to zero each time.

You can fix the first by changing the middle part of the for loop to size >= 0 (but be careful—if you later change it so that size is an unsigned type, because it doesn’t make sense for it to be negative, that code won’t work; it’s generally better to increment going up instead). You can fix the second by using i everywhere in the loop statement, and not changing size. You can fix the third by using i in the loop body, and not declaring a new variable inside the loop.

Daniel H
  • 7,223
  • 2
  • 26
  • 41
2

I noticed you used std::string so I used std function swap and string. Depending on if you consider this as a 'library'. There are several definitions of 'reverse'. You could reverse the word order in a string, or a pure char to char reversal like I wrote. Reversal could also mean changing character case, etc... but this is simply swap first and last. Then swap the 2nd and 2nd to last, then swap the 3rd and 3rd to last, etc...

So some points from your code. You only need to loop half the string length. The swap is from the ith and the ith to last. So the last is numCharacters - 1, thus the ith to last would be Last - i or numCharacters - 1 - i. I believe this is what you intended by using a farLeft(i) and a farRight(j) index.

#include <iostream>

void reverseStringInPlace(std::string &stringToReverse)
{
    int numCharacters = stringToReverse.length();
    for (int i=0; i<numCharacters/2; i++)
    { std::swap(stringToReverse[i], stringToReverse[numCharacters-i-1]); }
}

int main()
{
   std::string stringToReverse = "reversing a string";
   std::cout << stringToReverse << std::endl;
   reverseStringInPlace(stringToReverse);
   std::cout << stringToReverse << std::endl;
   return 0;
}

Output:

reversing a string
gnirts a gnisrever

JMan Mousey
  • 271
  • 2
  • 13
0

Changes made to the piece of code in question, it works.

 for (unsigned int i = size; size >= 0; size--) {

            revStr[j] = str[size];
            j++;

        }
Dana Amin
  • 93
  • 1
  • 1
  • 9