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?
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>
usingnamespace 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;
charconst v=sentence.at(x); //1)v=p 2) v=i 3) v=e
charconst 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;
}