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;
}