C C++ 字符串大小写转换

C C++ 字符串大小写转换

根据 ASCII 表进行转换

  • ASCII 表中,对应的大小写字母的 ASCII 值相差32,比如 A 为 65, a 则为 97

    #include <cstdio>
    #include <cstdint>
    #include <cstring>
    #include <cassert>
    
    char* StrToLower(char* str)
    {
        assert(nullptr != str);
        uint32_t i = 0;
        while (str[i]) {
            if ('A' <= str[i] && 'Z' >= str[i]) {
                str[i] += 32;
                // 或者
                // str[i] |= 0x20;
            }
            ++i;
        }
        return str;
    }
    
    char* StrToUpper(char* str)
    {
        assert(nullptr != str);
        uint32_t i = 0;
        while (str[i]) {
            if ('a' <= str[i] && 'z' >= str[i]) {
                str[i] -= 32;
                // 或者
                // str[i] &= ~0x20;
            }
            ++i;
        }
        return str;
    }
    
    int main(void)
    {
        // 需注意释放 strdup 分配的内存
        char str[] = "";
        printf("(%s) lower is (%s), upper is (%s)\n", str, StrToLower(strdup(str)),
               StrToUpper(strdup(str)));
        char str1[] = " fas  fadsd ";
        printf("(%s) lower is (%s), upper is (%s)\n", str1, StrToLower(strdup(str1)),
               StrToUpper(strdup(str1)));
        char str2[] = " AbcDf  fsadfW*&#)DDaa ";
        printf("(%s) lower is (%s), upper is (%s)\n", str2, StrToLower(strdup(str2)),
               StrToUpper(strdup(str2)));
        return 0;
    }
    

使用 toupper tolower 进行转换

  • C 库提供了函数 toupper 以及 tolower,可以方便的进行字母大小写转换。

    #include <ctype.h>
    #include <cstdio>
    #include <cstdint>
    #include <cstring>
    #include <cassert>
    
    char* StrToLower(char* str)
    {
        assert(nullptr != str);
        uint32_t i = 0;
        while (str[i]) {
            str[i] = tolower(str[i]);
            ++i;
        }
        return str;
    }
    
    char* StrToUpper(char* str)
    {
        assert(nullptr != str);
        uint32_t i = 0;
        while (str[i]) {
            str[i] = toupper(str[i]);
            ++i;
        }
        return str;
    }
    
    int main(void)
    {
        // 需注意释放 strdup 分配的内存
        char str[] = "";
        printf("(%s) lower is (%s), upper is (%s)\n", str, StrToLower(strdup(str)),
               StrToUpper(strdup(str)));
        char str1[] = " fas  fadsd ";
        printf("(%s) lower is (%s), upper is (%s)\n", str1, StrToLower(strdup(str1)),
               StrToUpper(strdup(str1)));
        char str2[] = " AbcDf  fsadfW*&#)DDaa ";
        printf("(%s) lower is (%s), upper is (%s)\n", str2, StrToLower(strdup(str2)),
               StrToUpper(strdup(str2)));
        return 0;
    }
    

对于 C++ string 可使用 transform

#include <cctype>
#include <cstdio>
#include <algorithm>
#include <string>

std::string StrToLower(std::string&& str)
{
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    return str;
}

std::string StrToUpper(std::string&& str)
{
    std::transform(str.begin(), str.end(), str.begin(), ::toupper);
    return str;
}

int main(void)
{
    std::string str = "";
    printf("(%s) lower is (%s), upper is (%s)\n", str.c_str(), StrToLower(std::string(str)).c_str(),
           StrToUpper(std::string(str)).c_str());
    std::string str1 = " fas  fadsd ";
    printf("(%s) lower is (%s), upper is (%s)\n", str1.c_str(), StrToLower(std::string(str1)).c_str(),
           StrToUpper(std::string(str1)).c_str());
    std::string str2 = " AbcDf  fsadfW*&#)DDaa ";
    printf("(%s) lower is (%s), upper is (%s)\n", str2.c_str(), StrToLower(std::string(str2)).c_str(),
           StrToUpper(std::string(str2)).c_str());
    return 0;
}

使用函数 strupr、strlwr,只能在 windows 下使用

#include <string.h>
#include <stdio.h>

int main(void)
{
    // 需注意释放 strdup 分配的内存
    char str[] = "";
    printf("(%s) lower is (%s), upper is (%s)\n", str, strlwr(strdup(str)),
           strupr(strdup(str)));
    char str1[] = " fas  fadsd ";
    printf("(%s) lower is (%s), upper is (%s)\n", str1, strlwr(strdup(str1)),
           strupr(strdup(str1)));
    char str2[] = " AbcDf  fsadfW*&#)DDaa ";
    printf("(%s) lower is (%s), upper is (%s)\n", str2, strlwr(strdup(str2)),
           strupr(strdup(str2)));
    return 0;
}
  • 26
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专注的罗哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值