大小写字母转换c语言使用if else if
时间: 2024-03-10 07:41:58 浏览: 37
在C语言中,可以使用if-else语句来进行大小写字母的转换。下面是一个示例代码:
```c
#include <stdio.h>
int main() {
char letter;
printf("请输入一个字母:");
scanf("%c", &letter);
if (letter >= 'a' && letter <= 'z') {
// 小写字母转换为大写字母
letter = letter - 32;
printf("转换后的字母为:%c\n", letter);
} else if (letter >= 'A' && letter <= 'Z') {
// 大写字母转换为小写字母
letter = letter + 32;
printf("转换后的字母为:%c\n", letter);
} else {
printf("输入的不是字母!\n");
}
return 0;
}
```
上述代码中,首先通过`scanf`函数获取用户输入的字母,并将其存储在`letter`变量中。后使用if-else if语句进行判断,如果输入的是小写字母,则将其转换为大写字母;如果输入的是大写字母,则将其转换为小写字母;如果输入的不是字母,则输出提示信息。
相关问题
pta大小写字母转换c语言
以下是将小写字母转换为大写字母的C语言代码示例:
```c
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
if (c >= 'a' && c <= 'z') {
c = c - 32; }
printf("%c %d", c, c);
return 0;
}
```
以下是将一个以“#”结束的字符串中的大小写字母互相转换的C语言代码示例:
```c
#include <stdio.h>
int main()
{
char s[100];
gets(s);
for (int i = 0; s[i] != '#'; i++) {
if (s[i] >= 'A' && s[i] <= 'Z') {
s[i] += 32;//大写变成小写
} else if (s[i] >= 'a' && s[i] <= 'z') {
s[i] -= 32;//小写变成大写
}
}
for (int i = 0; s[i] != '#'; i++) {
printf("%c", s[i]);
}
return 0;
}
```
字母大小写转换c语言
在C语言中,可以通过ASCII码表来实现字母大小写的转换。具体来说,将小写字母转换为大写字母,只需将小写字母的值减去32;将大写字母转换为小写字母,只需将大写字母的值加上32。以下是两个自定义函数的实现代码:
将小写字母转换为大写字母:
#include <stdio.h>
int my_toupper(int ch) {
if (ch >= 97 && ch <= 122)
return ch - 32;
else
return ch;
}
int main() {
char ch = 'a';
ch = my_toupper(ch);
printf("%c", ch);
return 0;
}
将大写字母转换为小写字母:
#include <stdio.h>
int my_tolower(int ch) {
if (ch >= 65 && ch <= 90)
return ch + 32;
else
return ch;
}
int main() {
char ch = 'A';
ch = my_tolower(ch);
printf("%c", ch);
return 0;
}