位操作符实现的大小写转换

case.cpp内容如下:

#include <iostream>
using namespace std;

char jc_tolower(char c) { return (c >= 'A' && c <= 'Z') ? (c | 0x20) : c; }
char jc_toupper(char c) { return (c >= 'a' && c <= 'z') ? (c & ~0x20) : c; }

int main(int argc, char **argv)
{
    char msg[] = "My name is Jackie.abcdefghijklmnopqrstuvvxyzABCDEFGHIJKLMNOPQRSTUVVXYZ";
    for (int i = 0; i < sizeof(msg) - 1; ++i)
        cout << jc_tolower(msg[i]);
    cout << endl;

    for (int i = 0; i < sizeof(msg) - 1; ++i)
        cout << jc_toupper(msg[i]);
    cout << endl;
    return 0;
}

运行结果如下:

posted @ 2020-07-30 16:48  jackie_astro  阅读(184)  评论(0编辑  收藏  举报