求助,如何用c++实现字母大小写转换

如题所述

在cctype头文件中有自带的toupper()和tolower()函数。

只需要

12345678910#include <cstdio>#include <cctype>using namespace std; int main(){    char c1 = 'a', c2 = 'B';    printf("%c toupper = %c\n", c1, toupper(c1));    printf("%c tolower = %c\n", c2, tolower(c2));    return 0;}

输出结果是:

a toupper = A
B tolower = b

或者可以自己实现一个这样的函数:

123456789int toupper(int ch){    if (ch < 'a' || ch > 'z') return -1;    return ch - 'a' + 'A';} int tolower(int ch){    if (ch < 'A' || ch > 'Z') return -1;    return ch - 'A' + 'a';}

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