fueanta

Reversed String by Words [File Method]

Sep 8th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. // cpp program : reversed string by words [file method]
  2. // AIUB SUMMER"15-16" FINAL CODEWRITING QUES.01
  3. // Author: HTA
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <conio.h>
  8.  
  9. using namespace std;
  10.  
  11. int word_number(string);
  12.  
  13. int main() {
  14.     string str;
  15.     cout << "String from User: "; getline(cin,str);
  16.     ofstream write; write.open("input.txt");
  17.     write << str << endl; write.close();
  18.     int words=word_number(str);
  19.     ifstream read("input.txt");
  20.     string str2[words];
  21.     for (int j=(words-1);true;j--) {
  22.         read >> str2[j];
  23.         if(read.eof()) break;
  24.     }
  25.     read.close(); string str3;
  26.     for (int j=0;j<words;j++) {
  27.         str3+=(str2[j]+" ");
  28.     }
  29.     write.open("reverseOutput.txt");
  30.     write << str3 << endl; write.close();
  31.     cout << "Reversed string: " << str3 << endl;
  32.     getch();
  33.     return 0;
  34. }
  35.  
  36. int word_number(string st) {
  37.     int counta = 0;
  38.     for (int i = 0; st[i]; i++)
  39.         if (st[i] == ' ')
  40.             counta++;
  41.     if (st[0] != '\0' && st[0] != ' ') counta++;
  42.     return counta;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment