C语言中转换大小写的函数121


C语言提供了两个函数用于转换字符串中字符的大小写:`toupper()`和`tolower()`。

`toupper()`函数

`toupper()`函数将字符串中的所有小写字母转换为大写字母。如果字符串包含非字母字符,则这些字符不会受到影响。语法:
```c
int toupper(int c);
```
参数:
* `c`:要转换的字符
返回值:
* 转换后的字符
示例:
```c
#include
#include
int main() {
char str[] = "Hello, world!";
int i;
for (i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("%s", str); // 输出:HELLO, WORLD!
return 0;
}
```

`tolower()`函数

`tolower()`函数将字符串中的所有大写字母转换为小写字母。如果字符串包含非字母字符,则这些字符不会受到影响。语法:
```c
int tolower(int c);
```
参数:
* `c`:要转换的字符
返回值:
* 转换后的字符
示例:
```c
#include
#include
int main() {
char str[] = "HELLO, WORLD!";
int i;
for (i = 0; str[i] != '\0'; i++) {
str[i] = tolower(str[i]);
}
printf("%s", str); // 输出:hello, world!
return 0;
}
```

其他大小写转换函数

除了`toupper()`和`tolower()`函数外,C语言还提供了以下大小写转换函数:* `isupper()`:检查字符是否为大写字母
* `islower()`:检查字符是否为小写字母
* `ispunct()`:检查字符是否为标点符号
* `isalpha()`:检查字符是否为字母
* `isblank()`:检查字符是否为空格或制表符
这些函数非常有用,可以用于各种文本处理任务,例如验证输入、格式化字符串以及比较字符串。

2024-11-15


上一篇:C 语言中高效输出判断结果

下一篇:C 语言字符串输入函数详解