Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cpp program : reversed string by words [file method]
- // AIUB SUMMER"15-16" FINAL CODEWRITING QUES.01
- // Author: HTA
- #include <iostream>
- #include <fstream>
- #include <conio.h>
- using namespace std;
- int word_number(string);
- int main() {
- string str;
- cout << "String from User: "; getline(cin,str);
- ofstream write; write.open("input.txt");
- write << str << endl; write.close();
- int words=word_number(str);
- ifstream read("input.txt");
- string str2[words];
- for (int j=(words-1);true;j--) {
- read >> str2[j];
- if(read.eof()) break;
- }
- read.close(); string str3;
- for (int j=0;j<words;j++) {
- str3+=(str2[j]+" ");
- }
- write.open("reverseOutput.txt");
- write << str3 << endl; write.close();
- cout << "Reversed string: " << str3 << endl;
- getch();
- return 0;
- }
- int word_number(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;
- }
Advertisement
Add Comment
Please, Sign In to add comment