Reverse sentence error

Write your question here.
Hi guys I am a newbie programmer (started like 7 months ago) and i need help with my program. It is sposed to reverse words like this:
cat
tac
but if i enter "pie" it will give me "3" can someone tell me how to solve 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
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
  Put the code you need help with here.
// BackWardsSentence.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int BackWords (string sentence);
int BackSentence (string sentence);
int _tmain(int argc, _TCHAR* argv[])
{
	string sentence;
	int choice;
	cout<< "What is your sentence" << endl;
	getline (cin,sentence);
	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;
	}
	if(choice==1)
	{
		cout<< "Your new sentence is" <<" "<<BackSentence(sentence)<<endl;
	}
	return 0;
}

int BackWords (string sentence)
{
	int length= sentence.length(); //3
	int x=0;
	int y=length-1; //2
	int a=0;
	while (x<length) //x<3
	 {
		string sv;
		string sb;
		string sy;
		char const v=sentence.at(x); //1)v=p 2) v=i 3) v=e
		char const b=sentence.at(y); //1) b=e 2)b=i 3) b=p
		sv = (v);
		sb = (b);
		sentence.replace(x,1,sv);
		sentence.replace(y,0,sb);
		x++;
		y--;
	} 
	return length;
}
int BackSentence (string sentence)
{
	int length = sentence.length();

	return length;
}
Last edited on
Both functions return the length of the string.
Thanks dumb error
Topic archived. No new replies allowed.