Read input from a file, capitalize first letter, make every other letter lowercase, and output into a separate file

前端 未结 2 941
無奈伤痛
無奈伤痛 2021-01-21 20:36

I am supposed to ask the user for two file names (input and output files). The contents from the input file should be read and the first letter of each sentence should be made u

2条回答
  •  生来不讨喜
    2021-01-21 21:15

    This part of your code should be changed:

            // if (isprint(ch)) {
            if (ch != '.') {
                if (new_sentence) {
                    outputFile.put(toupper(ch));
                }
                else {
                    outputFile.put(tolower(ch));
                }
                new_sentence = false;
            }
            else {
                new_sentence = true;
                outputFile.put(ch);
            }
    

    std::isprint() only checks if the character is printable.


    Full code:

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    int main() {
        string input_file;  // To hold input file name
        string output_File; // To hold output file name
        char ch;            // To hold character
        fstream inputFile;
        fstream outputFile;
    
        bool new_sentence = true;
    
        cout << "Enter input file name: " << endl;
        cin >> input_file;
    
        cout << "Enter output file name: " << endl;
        cin >> output_File;
    
        outputFile.open(output_File, ios::out);
        inputFile.open(input_file, ios::in);
    
        if (inputFile) {
            while (inputFile.get(ch)) {
                if (ch != '.') {
                    if (new_sentence) {
                        outputFile.put(toupper(ch));
                    }
                    else {
                        outputFile.put(tolower(ch));
                    }
                    new_sentence = false;
                }
                else {
                    new_sentence = true;
                    outputFile.put(ch);
                }
            }
            inputFile.close();
            outputFile.close();
       }
       else {
           cout << "Cannot open file(s)." << endl;
       }
    
       cout << "\nFile conversion complete." << endl;
    
       return 0;
    }
    

提交回复
热议问题