c语言字符串小写转大写程序
时间: 2023-06-22 13:23:52 浏览: 131
下面是一个 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()` 函数将其转换成大写字母,并输出结果。
相关问题
C语言字符串小写变大写
你可以使用C语言内置的toupper函数将字符串中的小写字符转换为大写字符。下面是一个简单的例子:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "hello, world!";
int i;
for (i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("转换后的字符串:%s\n", str);
return 0;
}
```
输出:
```
转换后的字符串:HELLO, WORLD!
```
c语言字符串字母小写转大写程序
下面是一个 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))`,用来判断当前字符是否是字母。只有在是字母的情况下,才需要将小写字母转成大写字母。其他的字符(如数字、标点符号等)则不做处理。
阅读全文
相关推荐















