c语言字符串字母小写转大写程序
时间: 2023-12-02 12:02:33 浏览: 80
下面是一个 C 语言字符串中字母小写转大写的示例程序:
```c
#include <stdio.h>
#include <ctype.h>
void to_upper(char *str) {
while (*str) {
if (isalpha(*str)) { // 判断字符是否是字母
*str = toupper(*str); // 将小写字母转成大写字母
}
str++;
}
}
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin);
to_upper(str);
printf("转换后的字符串为:%s", str);
return 0;
}
```
程序中依然使用了 `toupper()` 函数将字符串中的小写字母转成大写字母,但是在遍历字符串中的每个字符之前增加了一个判断:`if (isalpha(*str))`,用来判断当前字符是否是字母。只有在是字母的情况下,才需要将小写字母转成大写字母。其他的字符(如数字、标点符号等)则不做处理。