Home > Mobile >  Output whole sentence when reversing words in string c
Output whole sentence when reversing words in string c

Time:06-13

Have to create our own function that receives a sentence/sentences from an input file. It then should reverse the letters of each word individually and leaves all other (non-alphabetic) characters in the plaintext unchanged i.e "The cat sat on the mat!" will become "ehT tac tas no eht tam!". So I think I have found a way to reverse words individually but don't know how to find a way to output everything in one sentence. I feel like I would need to somehow use arrays or vectors to help store each word, and then output all the words together at the end, but I have not been successful. I also want to find a way for it to know when to stop and to output the blank spaces between words.

Here is my code so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std; 

void reverse(string input); 

int main(){
    ifstream inData; 
    ofstream outData; 

    string input; 

    inData.open("input.txt"); 
    outData.open("output.txt"); 

    while(getline(inData, input)){
        // cout << input; 
        outData << input; 
    }

   
    reverse(input);

    inData.close(); 
    outData.close(); 
    return 0; 
}

void reverse(string input){
  
    int counter =0;
    while(counter != 14){
    int idx = input.find(" ");
    cout << idx << endl;

    string word = input.substr(0, idx);
    cout << word << endl;
    string x;

    for (int i = idx-1; i >= 0; i--)
    {
        x= word.at(i);
        cout << x; 
    }

    cout << endl;
    input.erase(0,idx 1);

    cout << input << endl;

    cout << endl << "new" << endl;
    
    counter  ; 
    }
} 

CodePudding user response:

So here is code that can output the sentence from the input file, with each word being reversed and all these words then being outputted in one sentence in the order they were in the sentence. There is still some problems to it, like if there is a non-alphabetic character in the word, at the front or somewhere before the end, that character will be pushed to the back of the word, so this needs to be improved on. Feel free to nicely add constructively criticism if have any. However, think this still provides for the original question. Thank you all for the help.

#include <fstream>
#include <string>
#include <sstream>
using namespace std; 

void reverse(string input); 

int main(){
    ifstream inData; 
    ofstream outData; 

    string input; 

    inData.open("input.txt"); 
    outData.open("output.txt"); 

    while(getline(inData, input)){          //get sentence from input line
        reverse(input);                     //Reverse the sentence
        cout << endl;                       //end line for next sentence
    }

    inData.close(); 
    outData.close(); 
    return 0; 
}

void reverse(string input){
    string nonAlpha = "";
    bool end = false;
    bool nonLetter = true;

    while(end != true){         
    int idx = input.find(" ");                       //Finds index of blank space
    int lastIdx = input.size();                      //Finds the last index

    if(idx <= 0){                                    //If it is the last index, then that becomes idx
        idx = lastIdx;
        end = true;
    }

    string word = input.substr(0, idx);               //Word usually starts from index 0, until the blank space
    string x;

     
    for (int i = idx-1; i >= 0; i--){                  //Then outputs each letter according to the index of the word 
                                                      // but starts from last index and decrements thereby reversing the word
        if((isalpha(word.at(i))==false)){               //If that index is not a letter then it is stored and will be outputted after word 
           nonAlpha.push_back(word[i]);                 //However this can be a problem for non-letter if not at end of word
           nonLetter = true; 
        }
        else{
        x= word.at(i);
        cout << x;                                      //Prints the letter
        }
        
    }
    
    if(nonLetter == true){
    cout << nonAlpha;                                   //Prints the non-letter
    nonAlpha.erase();                                   //Erases whatever is inside for future non-letters
    nonLetter = false;                                  //Makes it false and will be used agaian when there is another non-letter
    }
    
    cout << " ";                                        //Space between words
    input.erase(0,idx 1);                               //Erases word when finished reversing and outputting it so can start next word

    }

} 
  • Related