[Date Prev][Date Next][Date Index]
[intv] Reverse the order of words.
//Reverse the order of words in a string.
//Save as: main.cpp and compile as: g++ -o main main.cpp
#include <iostream>
#include <string>
using namespace std;
int main(void){
string s1 = "I am a tea pot fat and stout, this is my handle this is my pout", s2;
int wordEnd;
wordEnd = s1.length() - 1;
for (int i = (s1.length() -1); i >= 0; i--){
if (s1[i] == ' '){
s2.append(s1, (i+1), wordEnd - i);
s2 += ' ';
wordEnd = i-1;
}
}
s2.append(s1, 0, wordEnd+1);
cout << s2 << endl;
}
Comments and corrections are appreciated and can be sent to
papers@mia.ece.uic.edu.
Click here for ©opyright information.
|