Help With Program


I need to print the string typed in by user off of keyboard, and take that string and reverse it(for example: "today is wednesday" to "wednesday is today"), I don't really know how to do it, and was hoping someone could help. I have some of it written out, but I can't figure out the rest

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

int nwords(string);
void reverse(string);


int main()
{
string next;
int count;



getline(cin,next);
while(cin)
{
count=nwords(next);
cout<<next<<endl;
cout<<count<<endl;
reverse(next);
getline(cin,next);


}
return 0;
}

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;
}

void reverse(string str){
int pos=0;
string word, newstr;

while(pos!=-1){
pos=str.find(" ",0);
word=str.substr(0,pos);
newstr=word+" "+newstr;
}

cout<<newstr<<endl;

return;

}
Last edited on
please resubmit your code using "[code]"
Topic archived. No new replies allowed.