从文件中读取输入,首字母大写,其他字母小写,并输出到一个单独的文件中

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

我应该向用户询问两个文件名(输入和输出文件)。应读取输入文件中的内容,并将每个句子的第一个字母设为大写,而每隔一个字母应设为小写。结果应存储在输出文件中。

我知道有一些方法可以使用 toupper 和 tolower 函数,这些函数包括指针、数组,甚至是字符的 ASCII 值,但我试图通过使用 if/else 让这段代码工作,同时语句,以及布尔语句。我得到了各种结果,从所有字母被大写到 none 字母被大写但是,我认为现在我在正确的轨道上使用代码并且只是忽略了我增加字符的方式导致代码在句号和 space.

后不大写
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
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 (isprint(ch)) {
                if (new_sentence) {
                    outputFile.put(toupper(ch));
                }
                else {
                    outputFile.put(tolower(ch));
                }
                new_sentence = false;
            }
            else {
                if (ch == '.') {
                   new_sentence = true;
                   outputFile.put(ch);
                }
            }
        }
        inputFile.close();
        outputFile.close();
   }
   else {
       cout << "Cannot open file(s)." << endl;
   }

   cout << "\nFile conversion complete." << endl;

   return 0;
}

使用我当前的代码,我可以将第一个句子的第一个字母大写,并将其他字母全部小写。我能够在输出文件中存储和显示结果。我的问题是第一个句子之后的每个其他句子的第一个字母不会更改为大写。这让我觉得问题出在这部分代码中:

if (new_sentence)
{
  outputFile.put(toupper(ch));
}

else
{
  outputFile.put(tolower(ch));
}

我是不是遗漏了什么?

您的这部分代码应该更改:

        // 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() 只检查字符是否可打印。


完整代码:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
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;
}

你有一个小的逻辑错误。

您首先需要检查字符是否为句点。你需要记住这个状态。如果下一个字符是字母,那么我们检查最近是否设置了新句子标志。在这种情况下,也只有在这种情况下,我们重置新句子标志并将字符转换为大写。

所有其他字母字符都将转换为小写。其他字符不会被转换。

在您的解决方案中,您总是重置新句子标志。即使,如果下一个打印字符是 space(最有可能是这种情况)。

请查看更新的解决方案:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
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 == '.') {
                new_sentence = true;
            }
            if (isalpha(ch)) {
                if (new_sentence) {
                    ch = toupper(ch);
                        new_sentence = false;
                }
                else {
                    ch = tolower(ch);
                }
            }
            outputFile.put(ch);
        }
        inputFile.close();
        outputFile.close();
    }
    else {
        cout << "Cannot open file(s)." << endl;
}

    cout << "\nFile conversion complete." << endl;

    return 0;
}

然后,请看一些进一步的改进:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>


int main() {
    // Will hold the input and output filename
    std::string filename;  

    // This is our flag to indicate that a new sentence will come
    bool newSentence = true;

    // Get input filename
    std::cout << "Enter input file name: " << "\n";
    std::cin >> filename;
    // And try to open the file
    std::ifstream inFile(filename);

    std::cout << "Enter output file name: " << "\n";
    std::cin >> filename;
    // And try to open the file
    std::ofstream outFile(filename);

    // Only convert, if the input and output file could be opened 
    if (inFile && outFile) {
        char ch;
        while (inFile.get(ch)) {
            if (ch == '.') {
                newSentence = true;
            }
            if (isalpha(ch)) {
                if (newSentence) {
                    ch = toupper(ch);
                    newSentence = false;
                }
                else {
                    ch = tolower(ch);
                }
            }
            outFile.put(ch);
        }
    }
    else {
        std::cout << "Cannot open file(s)\n";
    }
    std::cout << "\nFile conversion program complete\n";
    return 0;
}

以及完整的 "C++ with algorithm" 解决方案。这里的转换或转换是在一条语句中完成的

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <algorithm>
#include <iterator>

int main() {
    // Will hold the input and output filename
    std::string filename;  

    // Get input filename
    std::cout << "Enter input file name: " << "\n";
    std::cin >> filename;
    // And try to open the file
    std::ifstream inFile(filename);

    std::cout << "Enter output file name: " << "\n";
    std::cin >> filename;
    // And try to open the file
    std::ofstream outFile(filename);

    // Only convert, if the input and output file could be opened 
    if (inFile && outFile) {
        // Do the conversion
        std::transform(
            std::istreambuf_iterator<char>(inFile), 
            std::istreambuf_iterator<char>(),
            std::ostreambuf_iterator<char>(outFile),
            [newSentence = true](char c) mutable {
                if (c == '.') newSentence = true; 
                if (std::isalpha(c)) 
                    if (newSentence) { 
                        newSentence = false; 
                        c = std::toupper(c); }  
                    else c = std::tolower(c); 
                return c;
            }
        );
    }
    else {
        std::cout << "Cannot open file(s)\n";
    }
    std::cout << "\nFile conversion program complete\n";
    return 0;
}

但是如果最后一个解决方案增加了额外的价值呢?我不知道 。 . .