Help with Printing string in reverse order

Hey guys I am new at c++ I was just wondering if someone could show me how to print a string in reverse order(for example: "today is Wednesday " to "Wednesday is today"). My professor said we should begin with a null string and build it one word at a time. i have been working on this for weeks, and cannot figure it out. If someone can help me out,I would greatly appreciate it!!!

Thank You,

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int nwords(string);

int main()
{
string next;
int count;

ifstream mohsin("line.txt");

getline(mohsin,next);
if(mohsin)
{
count=nwords(next);
cout<<next<<endl;
getline(mohsin,next);
}
}

int nwords(string str)
{
int nextblank;
int nw=0;

nextblank=str.find(" ",0);

while(nextblank>0)
{
nw++;
str=str.substr(nextblank+1);
nextblank=str.find(" ",0);
}

return nw;
}

string reverse(string str){}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <sstream>
#include <string>


std::string reverse_words(const std::string& origin)
{
    std::istringstream source(origin);
    std::string result;
    std::string temp;
    while(source >> temp)
        result = temp + " " + result;
    return result;
}


int main()
{
    std::cout << reverse_words("today is Wednesday ");
}
Last edited on
Topic archived. No new replies allowed.