Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Project4MCS360
- //
- // Created by Peter Apostle on 12/5/18.
- // Copyright © 2018 MCS360. All rights reserved.
- //
- #include <iostream>
- #include <stack>
- #include <string>
- #include <fstream>
- using namespace std;
- void convertFile(){
- ifstream myFile;
- ofstream outputFile;
- myFile.open("project4text.txt");
- stack<string> wholeFile;
- stack<char> splitWords;
- stack<char> reversedSplitWords;
- string wordInput;
- string strPop;
- while (myFile >> wordInput){
- wholeFile.push(wordInput);
- }
- myFile.close();
- while (!wholeFile.empty()){
- strPop = wholeFile.top();
- cout << strPop << endl;
- wholeFile.pop();
- for (int i = 0; i < strPop.length(); i++){
- char temp;
- temp = strPop[i];
- splitWords.push(temp);
- }
- splitWords.push(' ');
- }
- while (!splitWords.empty()){
- char temp;
- temp = splitWords.top();
- reversedSplitWords.push(temp);
- }
- outputFile.open("project4text2.txt");
- while (!reversedSplitWords.empty()){
- cout << reversedSplitWords.top();
- outputFile << reversedSplitWords.top();
- reversedSplitWords.pop();
- }
- }
- int main() {
- convertFile();
- return 0;
- }
- /*
- Program trace:
- Read file in one function, each word pushed to a stack
- i.e. "AB CD EF" -> stack has [EF | CD | AB]
- Pop from first stack and set strPop = popped value (EF)
- call strPop[i] for loop to push to second stack [E | F]
- pop from second stack to third stack to reverse letters [F | E]
- pop from third stack to write to file, when third stack is empty add space character
- MAKE SURE TO INCLUDE A SPACE AT THE END OF EACH WORD.
- Repeat until all words written to new file.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement