C++ Program That Reverses A Word/Sentence

Write your question here.
Hello everyone i was tasked to write a program that takes a word/sentence then reverses the order of the word/sentence. I.E.:
I like c++
++c like I
I cannot use #algorithms.What I did didn't work. I can't think of a solution but here is my starting code.
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
69
70
71
72
 // BackWardsSentence.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm> 

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;
	string newsentence;
	int length = sentence.length;
	cout<< "What is your sentence" << endl;
	getline (cin,sentence);
	string newsentence;
	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;
	for(int x=0;x<length;x++)
	{

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

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




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

	return length;
}
Last edited on
closed account (j3Rz8vqX)
Example:
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string reverseWords(string str)
{
    int posStart=0,posEnd=0;
    vector<string> Data;
    do
    {
        posEnd=str.find(' ',posStart)-posStart;
        if(posEnd>0)
            Data.push_back(str.substr(posStart,posEnd));
        else
            Data.push_back(str.substr(posStart));
        posStart+=posEnd+1;
    }while(posEnd>0);
    str="";
    for(int i=0;i<Data.size();++i)
        str += Data[Data.size()-1-i]+' ';
    return str;
}
string reverseLetters(string str)
{
    string temp;
	for(int i=0;i<str.size();++i)
        temp += str[str.size()-1-i];
    return temp;
}
int main()
{
    string str;
    while(cout<<"Enter a string: "&&getline(cin,str)&&str.size()<1);
    cout<<"\nReversed string: "<<reverseLetters(str)<<'\n';
    cout<<"\nReversed words: "<<reverseWords(str)<<'\n';

    cout<<"\nPress <enter> to exit console: ";
    cin.get();
    return 0;
}
Enter a string: This is a string.

Reversed string: .gnirts a si sihT

Reversed words: string. a is This

Press <enter> to exit console:

-Used vector to contain words before reversal; could be done with arrays, but would require the number of words, or dynamic allocations.
Is there any simpler way to do this? I don't really understand this part : posEnd=str.find(' ',posStart)-posStart; I am a new programmer sorry...
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string reverseWords(string str)
{
    int posStart=0,posEnd=0;
    vector<string> Data;
    do
    {
        for(posEnd=posStart;posEnd<str.size();++posEnd)
            if(str[posEnd]==' ')
                break;
        posEnd-=posStart;//We want difference between (start position and end position) for use below:
        if(posEnd>0)
            Data.push_back(str.substr(posStart,posEnd));//start position and number of characters
        else
            Data.push_back(str.substr(posStart));
        posStart+=posEnd+1;
    }while(posStart<str.size());
    str="";
    for(int i=0;i<Data.size();++i)
        str += Data[Data.size()-1-i]+' ';
    return str;
}
Enter a string: This is a string

Reversed string: gnirts a si sihT

Reversed words: string a is This

Press <enter> to exit console:

Finding the spaces and take a substring of the current string:
http://www.cplusplus.com/reference/string/string/substr/

Afterwards, accumulate the words in reverse order, into a single string and return the string.

A simpler way? possibly, but it would depend on your design; string parsing was applied both times, in the previous post and the above.
Topic archived. No new replies allowed.