Crash in Program Reversing Word

Write your question here.
Hey everybody I have some code that is supposed to reverse the order of a word. My program crashes and says: Debug Assertion Failed Expression: string subscript out of range. This piece of code makes my program crash : newsentence[end]=sentence[start];
Can someone please give me a fix.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 // BackWardsSentence.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string BackWords (string sentence);
int BackSentence (string sentence);
string BackWords1 (string sentence);

int _tmain(int argc, _TCHAR* argv[])
{
	string sentence;
	int choice;
	cout<< "What is your sentence" << endl;
	getline (cin,sentence);
	int length = sentence.length();
	cout<< "You entered" << " "<< sentence;
	cout<< " "<<"If you would like to reverse letters enter 0 if you would like to reverse words enter 1"<<endl;
	cin>> choice;
	

	if(choice==0)
	{
	cout<< "Your new sentence is " << " " <<BackWords(sentence)<< endl;
	}
	
	return 0;
}

string BackWords (string sentence)
{
	int length= sentence.length(); //3
	string newsentence;
	int x=0;
	int y=length-1; //2
	int a=0;
	int start=0;
	int end=length;

	while (x<y)
	{
		newsentence[end]=sentence[start];
		start++;
		end--;

	}
	/*while (x<length-1) //x<3
	 {
		 
		string sv;
		string sb;
		string sy;
		char const v=sentence.at(x); 
		char const b=sentence.at(y);
		sv = (v);
		sb = (b);
		sentence.replace(x,1,sb);
		sentence.replace(y,1,sv);
		x++;
		y--;
		
	}
	*/
	return sentence;
}
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
	while (x<y)
	{
		newsentence[end]=sentence[start];
		start++;
		end--;

	}

While loops repeat while true.
In the above, x will always be less than y, since x never increments or changes in the loop; error.

You may want to consider modifying x or y within the loop; or use end or start.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
    string newsentence,sentence="Hello world";
    int start=0, end=sentence.size()-1;
    while (start<=end)
        newsentence+=sentence[end--];
    cout<<newsentence<<'\n';
    return 0;
}
dlrow olleH

Process returned 0 (0x0)   execution time : 0.018 s
Press any key to continue.

Also, newsentence has not been assigned a value. Modifying characters that do no exist, may result in errors; the work around I provided was to concatenate characters.

Have fun.
Thank you
Can you please explain this part: newsentence+=sentence[end--];?
I don't understand the += operator and the [end--]
closed account (j3Rz8vqX)
newsentence+=sentence[end--]

is equivalent to:

1
2
newsentence = newsentence + sentence[end];
end=end-1;


Tutorial:
http://www.cplusplus.com/doc/tutorial/operators/

Section:
Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

Example:

newsentence = "" + 'H';
newsentence = "H" + 'e';
newsentence = "He" + 'l';
newsentence = "Hel" + 'l';
newsentence = "Hell" + 'o';
//...
newsentence = "Hello worl" + 'd';
Last edited on
Thank you so much!
Topic archived. No new replies allowed.