Why doesn't code reverse string?

Hello everyone, I unfortunately had to take a little break from learning programming so I have forgotten a few things and a little rusty, so I apologize if the answer is blatantly obvious.

I had to write a small program that takes a string that the user enters and reverses it. After spending awhile trying I got it working with this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s = "";
	string r = "";

	cout << "Please enter a string: ";
	getline(cin, s);

	for (int i = 0; i < s.length(); i++)
		r = s[i] + r;

	cout << "Reversed string = " << r << endl;

	cout << endl;
	system("Pause");
	return 0;
}


But before I got that I came up with this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s = "";
	int cnt = 0;

	cout << "Please enter a string: ";
	getline(cin, s);

	string r = s;

	for (int i = s.length(); i > 0; i--)
	{
		r[cnt] = s[i];
		cnt++;
	}

	cout << "Reversed string = " << r << endl;

	cout << endl;
	system("Pause");
	return 0;
}


It reverses most of the string but doesn't show the first character of the original string. For instance Hello comes out as olle. I've also tried some slight variations such as..

1
2
for (int i = s.length() + 1; i > 0; i--)
for (int i = s.length(); i >= 0; i--)


I am wondering why this code won't work correctly it seems like it should to me.


Thanks.
Maybe this will help
1
2
3
4
5
6
7
string original = "original string";
string reversed;
for ( int count = original.size()-1; count>=0; count--)
{
   reversed += original [count];
} 


We start at size ()-1 because we can't access thr char at size () because there's nothing there. We check >=0 to make sure we include the first char
Last edited on
closed account (jvqpDjzh)
You can do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string Reverse(std::string str="")
{
    if(str.length() > 1)//we don't need to reverse if the string is empty or 1
//it has to have at least 2 characters to make sense to reverse it
    {
        std::string newStr = "";
        for(int i=str.length() -1; i>=0; i--)
            newStr += str.at(i);
//at() is similar to str[i], but this last one doesn't throw an exception
        return newStr;
    }
    return str;//returns the string if its length <= 1
}
Last edited on
closed account (j3Rz8vqX)
You'd want to assign "i" the last value, not its termination.
And you'd want to include its entry [0].
1
2
3
4
5
	for (int i = s.length()-1; i >= 0; i--)
	{
		r[cnt] = s[i];
		cnt++;
	}


Assumption:
string == "hello"
[0] = 'h'
[1] = 'e'
[2] = 'l'
[3] = 'l'
[4] = 'o'

s.length() returns [5], but you wanted [4] to be the first argument, otherwise you'd get: 'garbage'+'o'+'l'+'l'+'e'+'h' == [5][4][3][2][1][0]

And because [0] was not inclusive: i > 0;
you'd get: 'garbage'+'o'+'l'+'l'+'e' == [5][4][3][2][1]

[4][3][2][1][0] == 'o'+'l'+'l'+'e'+'h'

Welcome back.
Thanks guys.

I can't believe I forgot about starting at 0 and the '\0' that would be at the very end of the string. Well, I didn't really forget since I started at 0 with..

1
2
3
4
5
6
int cnt = 0;
for (int i = s.length(); i > 0; i--)
	{
		r[cnt] = s[i];
		cnt++;
	}


So I'm really not sure how that slipped by.
Topic archived. No new replies allowed.