Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

C++ program to revere sentence

$
0
0
Hello everyone I had been tasked to create a program that reverses a word i.e.
cat
tac
Can someone tell me how to make my program non case-sensitive using vectors?
Code:
// 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);
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;
}

string BackWords (string sentence)
{
int length= sentence.length(); //3
int x=0;
int y=length-1; //2
int a=0;
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);
if(x==0)
{
sentence.replace(x,1,sb);
sentence.replace(y,1,sv);
}
if (x==1)
{
sentence.replace(x,1,sb);
sentence.replace(y,1,sv);
}
x++;
y--;
}
return sentence;
}

/*string BackWords1 (string sentence) {
int start = 0;
int end = sentence.length() - 1;
while (start < end) {
char temp = sentence[end];
sentence[end] = sentence[start];
sentence[start] = temp;
++start;
--end;
}
return sentence;
}*/


int BackSentence (string sentence)
{
int length = sentence.length();

return length;
}

Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images