2024年4月15日发(作者:)
c语言字符串大小写转换的函数
以下为实现字符串大小写转换的函数:
c
#include
#include
#include
将字符串转为小写
char* str_to_lower(char* str)
{
int len = strlen(str);
for (int i = 0; i < len; i++) {
str[i] = tolower(str[i]);
}
return str;
}
将字符串转为大写
char* str_to_upper(char* str)
{
int len = strlen(str);
for (int i = 0; i < len; i++) {
str[i] = toupper(str[i]);
}
return str;
}
int main()
{
char str[] = "Hello World!";
printf("%sn", str_to_lower(str)); 输出: hello world!
printf("%sn", str_to_upper(str)); 输出: HELLO WORLD!
return 0;
}
以上代码中,函数 `str_to_lower` 和 `str_to_upper` 分别用于将字符串转换为
小写和大写格式。
具体实现思路是使用 C 标准库中的 `tolower` 和 `toupper` 函数,将每个字
符逐一转换即可。转换之后需要将原字符串返回,因此返回值类型设置为 `char*`。
示例代码中,定义了一个字符串 `str`,将其先转为小写,再转为大写,并输出
结果。运行程序,可以看到输出结果为:
hello world!
HELLO WORLD!
发布者:admin,转转请注明出处:http://www.yc00.com/web/1713182355a2199118.html
评论列表(0条)