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

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
    ifstream infile("txt");
    if (!infile)
    {
        cerr << "Can't open file." << endl;
    }
 
    char ch;
    while (infile.get(ch))
    {
        if (ch >= 'a' && ch <= 'z')
        {
            ch -= 32;
        }
        else if (ch >= 'A' && ch <= 'Z')
        {
            ch += 32;
        }
        cout << ch;
    }
    infile.close();
    return 0;
}


python的实现

>>> "This Is Test".swapcase() #Œ大小写互换
'tHIS iS tEST'


你可能感兴趣的:(从一个文本文件读取正文,将其中的小写字母转化成大写字母,大写字母转换成小写字母,其他字符不变。)