program_4_10.cpp

来自「清华关于C++ 的程序讲义 值得一看 关于算法」· C++ 代码 · 共 43 行

CPP
43
字号
// Program 4.10: Echo input to standard output
// converting uppercase to lowercase along the way
#include <iostream>
#include <string>
using namespace std;
int main() {
    // prepare for processing text
    bool MoreLinesToProcess = true;
    while (MoreLinesToProcess) {
        // process next line
        bool MoreCharactersOnCurrentLine = true;
        cout << "Please type a line of text: ";
        while (MoreCharactersOnCurrentLine) {
            // process next character on current line
            char CurrentCharacter;
            if (cin.get(CurrentCharacter)) {
                // process current character on current line
                if (CurrentCharacter == '\n') {
                    // found newline character that ends line
                    MoreCharactersOnCurrentLine = false;
                }
                else if ((CurrentCharacter >= 'A')
                          && (CurrentCharacter <= 'Z')) {
                    // CurrentCharacter is uppercase
                    CurrentCharacter = CurrentCharacter - 'A' + 'a';
                    cout << CurrentCharacter;
                }
                else {  // nonuppercase character
                    cout << CurrentCharacter;
                }
            }
            else { // no more characters
                MoreCharactersOnCurrentLine = false;
                MoreLinesToProcess = false;
            }
        }
        // finish processing of current line
        cout << endl;
    }
    // finish overall processing
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?