C语言输入一个字符串小写转大写
时间: 2023-07-07 07:31:05 浏览: 119
在 C 语言中,可以使用 `ctype.h` 头文件中的 `toupper()` 函数将字符串中的小写字母转换为大写字母。以下是一个 C 代码示例:
```c
#include <stdio.h>
#include <ctype.h> // 包含 toupper 函数
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, 100, stdin); // 从标准输入获取字符串
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]); // 将小写字母转换为大写字母
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
在这个示例中,我们首先定义一个 `char` 类型的数组 `str` 来存储用户输入的字符串。然后使用 `fgets()` 函数从标准输入获取字符串,遍历字符串中的每个字符,使用 `toupper()` 函数将小写字母转换为大写字母。最后,使用 `printf()` 函数将转换后的字符串输出到屏幕上。需要注意的是,`fgets()` 函数会将字符串的结尾符 `\n` 也读入,因此在字符串输出时需要手动添加 `%s` 格式控制符。
相关问题
c语言输入一个字符串小写转大写
在 C 语言中,可以使用 `ctype.h` 头文件中的 `toupper()` 函数将字符串中的小写字母转换为大写字母。以下是一个 C 代码示例:
```c
#include <stdio.h>
#include <ctype.h> // 包含 toupper 函数
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, 100, stdin); // 从标准输入获取字符串
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]); // 将小写字母转换为大写字母
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
在这个示例中,我们首先定义一个 `char` 类型的数组 `str` 来存储用户输入的字符串。然后使用 `fgets()` 函数从标准输入获取字符串,遍历字符串中的每个字符,使用 `toupper()` 函数将小写字母转换为大写字母。最后,使用 `printf()` 函数将转换后的字符串输出到屏幕上。需要注意的是,`fgets()` 函数会将字符串的结尾符 `\n` 也读入,因此在字符串输出时需要手动添加 `%s` 格式控制符。
c语言字符串小写转大写程序
下面是一个 C 语言字符串小写转大写的示例程序:
```c
#include <stdio.h>
#include <ctype.h>
void to_upper(char *str) {
while (*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()` 函数将字符串中的小写字母转成大写字母。函数 `to_upper()` 接受一个指向字符串的指针,然后遍历字符串中的每个字符,将小写字母转成大写字母。 `main()` 函数中获取用户输入的字符串,调用 `to_upper()` 函数将其转换成大写字母,并输出结果。