Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Cpp program to get the reversed version of a sentence [reversed by words]
- // created on June/July, 2016
- // Author: fueanta
- #include <iostream>
- #include <conio.h>
- using namespace std;
- int total_words(string);
- string get_words(string &st);
- int main(void) {
- string str1,str2,str3;
- cout << "Given String: ";
- getline(cin,str1);
- int words= total_words(str1);
- cout << "Total Words: " << words << endl;
- string st[words]; str2=str1;
- for (int i=(words-1);i>=0;i--) {
- st[i]= get_words(str2);
- }
- for (int i=0;i<words;i++) {
- str3+=(st[i]+" ");
- }
- cout << "Reversed String: " << str3 << endl;
- getch();
- return 0;
- }
- int total_words(string st) {
- int counta=0;
- for(int i=0; st[i] ;i++) {
- if(st[i]==' ')
- counta++;
- }
- if ((st[0]!='\0') || (st[0]!=' '))
- counta++;
- return counta;
- }
- string get_words(string &st) {
- string str; int i=0;
- while (st[i]) {
- if (st[i]!= ' ') {
- str+=st[i];
- st.erase(i,1);
- }
- if (st[i]== ' ') {
- st.erase(i,1);
- break;
- }
- }
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement