从文件中读取输入,将首字母大写,将其他每个字母都小写,然后输出到单独的文件中 [英] Read input from a file, capitalize first letter, make every other letter lowercase, and output into a separate file

查看:137
本文介绍了从文件中读取输入,将首字母大写,将其他每个字母都小写,然后输出到单独的文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该问用户两个文件名(输入和输出文件)。应读取输入文件中的内容,每个句子的首字母应大写,而其他字母应小写。然后将结果存储在输出文件中。

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 uppercase while every other letter should be made lowercase. The results should then be stored in the output file.

我知道有很多方法可以使用toupper和tolower函数,包括指针,数组甚至是ASCII值字符,但我试图通过使用if / else和while语句以及布尔语句来使此代码正常工作。我得到了各种各样的结果,从所有大写字母到大写字母都没有,但是,我认为现在我在代码上处于正确的轨道,并且只是忽略了通过字符递增导致代码出现错误的方式

I am aware that there are ways of using the toupper and tolower functions that include pointers, arrays, or even ASCII values of chars but I am trying to get this code to work by using if/else and while statements, as well as boolean statements. I have had various results ranging from all the letters being capitalized to none of the letters being capitalized however, I think right now I am on the right track with the code and am just overlooking the way I am incrementing through the characters causing the code to not capitalize after a period and a 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;
}

使用当前代码,我可以将第一句的首字母大写并将其他所有字母都小写。我能够在输出文件中存储并显示结果。我的问题是,第一个句子之后的所有其他句子的第一个字母都不会变为大写。这使我认为问题出在代码的这一部分:

With my current code I am able to capitalize the first letter of the first sentence and make every other letter lowercase. I am able to store and show the results in the output file. My issue is that the first letter of every other sentence after the first one won't change to uppercase. This makes me think the issue is in this part of the code:

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

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

我在这里错过了什么吗?

Am I missing something here?

推荐答案

您有一个较小的逻辑错误。

You have a minor logical error.

您首先需要检查字符是否为句点。您需要记住这种状态。如果下一个字符为alpha,则我们检查最近是否已设置新闻句标记。在这种情况下,并且只有在这种情况下,我们才重置新的句子标志并将字符转换为大写。

You first need to check, if the character is a period. This state you need to remember. If then a next character isalpha, then we check, if recently the newsentence flag has been set. In this case, and only in this case, we reset the new sentence flag and convert the character to uppercase.

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

All other alpha characters will be converted to lowercase. Other charcaters will not be converted.

在您的解决方案中,您始终会重置新闻输入标志。即使下一个打印字符是空格(最容易出现的情况)。

In your solution you always reset the newsentence flag. Even, if the next print character is a space (Which is most liekly the case).

请查看更新的解决方案:

Please see updated solution:

#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;
}

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

And then, please see some further improvements:

#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 ++解决方案。这里,转换或转换是在一个语句中完成的。

And the full blown "C++ with algorithm" solution. Here the conversion, or transformation is done in one statement

#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;
}

但是,如果最后一个解决方案增加了附加值?我不确定 。 。

But if the last solution adds additional value? I am not sure . . .

这篇关于从文件中读取输入,将首字母大写,将其他每个字母都小写,然后输出到单独的文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆