c语言实现输入字母判断大小写 大写转换为小写
时间: 2023-10-23 14:17:35 浏览: 229
可以使用C语言中的toupper()和tolower()函数进行大小写转换。下面是一个示例程序:
```
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
printf("请输入一个字母:\n");
scanf("%c", &c);
if (islower(c)) {
c = toupper(c);
printf("转换后的结果为:%c\n", c);
} else if (isupper(c)) {
c = tolower(c);
printf("转换后的结果为:%c\n", c);
} else {
printf("输入的不是字母!\n");
}
return 0;
}
```
这个程序会要求用户输入一个字母,然后使用islower()和isupper()函数判断是大写还是小写字母,再使用toupper()和tolower()函数进行转换。如果输入的不是字母,则程序会提示错误。
相关问题
用c语言实现字字符串大小写转换,大写转小写,小写转大写
你可以使用C语言的标准库函数`toupper`和`tolower`来实现字母的大小写转换。下面是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
void convertCase(char *str) {
while (*str) {
if (isupper(*str)) {
*str = tolower(*str); // 大写转小写
} else if (islower(*str)) {
*str = toupper(*str); // 小写转大写
}
str++;
}
}
int main() {
char str[100];
printf("输入字符串: ");
fgets(str, sizeof(str), stdin);
convertCase(str);
printf("转换后的字符串: %s", str);
return 0;
}
```
在这个例子中,我们定义了一个`convertCase`函数,它接受一个指向字符串的指针,并将字符串中的字母进行大小写转换。在`main`函数中,我们首先获取用户输入的字符串,然后调用`convertCase`函数进行大小写转换,并打印转换后的结果。
请注意,这个例子只对字母进行大小写转换,其他字符不受影响。另外,输入的字符串长度不能超过定义的数组大小。
大小写字母转换 C语言 输入大写字母 输出对应的小写字母 输出时最小宽度三位
A:
```c
#include <stdio.h>
int main() {
char upper;
scanf("%c", &upper);
// 大写字母转换成小写字母
char lower = upper + ('a' - 'A');
printf("%3c", lower);
return 0;
}
```
这段代码先读入一个大写字母,然后将其转换成小写字母,并使用 `%3c` 进行输出,保证了输出宽度为 3。大写字母转换成小写字母的方法是将其 ASCII 码加上 `'a' - 'A'`,这是由于大写字母和小写字母在 ASCII 码表上是相邻的,其 ASCII 码值差为 32,因此加上这个值即可实现大小写字母的转换。