Advertisement
mqpanda

Untitled

Feb 11th, 2023
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     string filename;
  10.     cout << "Enter the name of the file: ";
  11.     cin >> filename;
  12.    
  13.     ifstream file(filename);
  14.     if (!file.is_open()) {
  15.         cerr << "Error: could not open file " << filename << endl;
  16.         return 1;
  17.     }
  18.    
  19.     string line;
  20.     while (getline(file, line)) {
  21.         string sentence = "";
  22.         for (int i = line.length() - 1; i >= 0; i--) {
  23.             if (line[i] == ' ' || i == 0) {
  24.                 if (i == 0) {
  25.                     sentence = line[i] + sentence;
  26.                 }
  27.                 for (int j = 0; j < sentence.length(); j++) {
  28.                     cout << sentence[j];
  29.                 }
  30.                 cout << endl;
  31.                 sentence = "";
  32.             }
  33.             else {
  34.                 sentence = line[i] + sentence;
  35.             }
  36.         }
  37.         cout << endl;
  38.     }
  39.    
  40.     file.close();
  41.    
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement