如何从一个文本文件读取正文将之中的小写字母转化成大写字母将其中大写字母转换成小写字母.其他字符不变,

用C++做
从一个文本文件读取正文,将其中的小写字母转化成大写字母,大写字母转换成小写字母,其他字符不变,然后输出到另一个文本文件中保存。
要求:(1)用一个子函数完成转换功能

确定是C++? 如果是下面是一个简单实现的例子。 定义了一个函数,输入和输出文件可根据需要自行设定。

#include <iostream>
#include <algorithm>
#include <fstream>

using namespace std;

int CaseConvert(string infile, string outfile);

struct CaseReverse {
    int operator()(const int &c) {
        if (islower(c))
           return toupper(c);
        if (isupper(c))   
           return tolower(c);
        else
           return c;   
    }
};


int main(int argc, char** argv) 
{
    string infile("in.txt");
    string outfile("out.txt");
    int retval;
    
    retval = CaseConvert(infile, outfile);
    if (!retval)
       cout << "Done!" << endl;
    else
       cout << "Failed. error code " << retval << endl;
        
    return 0;
}

int CaseConvert(string infile, string outfile)
{
    ifstream ifs(infile.c_str());
    ofstream ofs(outfile.c_str());
    string buffer;
    int retcode = 0;
    
    if (!ifs)
       return -1;
       
    if (!ofs)   
       return -2;
       
    while (::getline(ifs, buffer))
    {
        transform(buffer.begin(), buffer.end(), buffer.begin(), 
               CaseReverse() ); 
        ofs << buffer << endl;       
    }
    if (!ifs.eof())
       retcode = -3;
    
    ifs.close();
    ofs.close();
        
    return retcode;
}

温馨提示:答案为网友推荐,仅供参考
相似回答