用ctype.h中的函数tolower和toupper.前者以大写的字符作为参数,返回相应的小写字符;后者以小写的字符作为参数,返回相应的大写字符.
#include ctype.h
#include stdio.h
int main()
{
char c = 'A';
printf("%c", tolower(c)); //a
c = 'b';
printf("%c", toupper(c)); //B
return 0;
}
如果没有相应的大小写,函数会返回字符本身.
char c = '0';
printf("%c", tolower(c)); //0
printf("%c", toupper(c)); //0
有三种方式可以解决c语言大小写字母的转换
①使用C语言提供的函数:toupper(),tolower()
使用这两个函数需要引入头文件:#includectype.h
示例代码:
输入如下
因为大小写之间的ASCII码值的差值是固定的,所以呢可以使用'a'-'A'来获取差值.
实例代码:
输出如下:
异或:相同为0,不同为1. ?也就是说,任何数字与0异或都是它本身.
输入如下:
希望可以帮到你...
#include?stdio.h
void?str_trans(char?c[])
for(int?i=0;c[i];i◆◆)
if(c[i]='z'?c[i]='a')
c[i]=(c[i]-'a')◆'A';
}else?if(c[i]='A'c[i]='Z')
c[i]=(c[i]-'A')◆'a';
int?main()
{?char?s[101];
gets(s);
str_trans(s);
puts(s);
scanf("%s",s);
return?0;
在C语言中转换大小写字母,可用ctype.h头文件中声明的函数toupper和tolower.
toupper:
int toupper(int c);
若c为小写字母,则将其转换为大写字母;否则,不转换,直接返回c.
tolower:
int tolower(int c);
若c为大写字母,则将其转换为小写字母;否则,不转换,直接返回c.
如果是日常使用, 那么可以考虑直接调用ctype.h里定义的函数
以上就是土嘎嘎小编为大家整理的c语言字符大小写转化函数相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!