My String Function isn't working

The function Reverse does seem to work. Its supposed to take the words in a sentence and then reverse them. For example: "This is nice" to "Nice is this."
When I type "This is nice" I get " niceniceis nice"
I can't seem to find the problem.
Thanks in advance
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
#include <iostream>
#include <string>
using namespace std;

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

int main()
{
    cout << "Enter first words" << endl;
    int numwords;
    string next;
    getline (cin,next);
    while(cin){
        numwords = nwords(next);
        cout <<"Number of words: "<<numwords<<endl;
        cout <<"Reverse version: "<<reverse(next)<<endl;
        cout<<"Enter next line"<<endl;
        getline(cin,next);
    }
    return 0;
}

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

string reverse (string str) {
string result="";
int nextblank;
nextblank = str.find(" ",0);
while(nextblank>0){
    str = str.substr(nextblank+1);
    result.insert(0,str);
    nextblank = str.find(" ",0);
}
result.insert(0,str);
return result;
}
I just put some cout statements into the reverse function to show what's happening with the str and result variables.

Enter first words 
this is nice 
Number of words: 3 
next blank: 4 
str: is nice 
result: is nice 
next blank: 2 
str: nice 
result: niceis nice 
next blank: -1 
Reverse version: niceniceis nice 
Enter next line
Topic archived. No new replies allowed.