怎么用C语言里函数转换大小写?

Python030

怎么用C语言里函数转换大小写?,第1张

用<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

}

如果没有相应的大小写,函数会返回字符本身。

#include <ctype.h>

#include <stdio.h>

int main()

{

char c = '0'

printf("%c", tolower(c))//0

printf("%c", toupper(c))//0

return 0

}

由于大写字母与小写字母之间的差值为 32,因此小写字母转换为大写字母的方法就是将小写字母的 ASCII 码值减去 32,便可得到与之对应的大写字母。

利用 getchar 函数从键盘上输入一个小写字母,并将其赋给一个字符变量 a;然后将 a—32 的值赋给字符变量 b;最后进行输出,输出时先输出字母,再将字母以整数形式输出。其具体步骤如下:

① 定义两个字符变量 a、b;

② a=get char();

③ b=a—32;

④ 打印输出。

程序代码

#include <stdio.h>

int main()

{

char a,b

printf("输入一个小写字母:\n")

a=getchar()

b=a-32

printf("转换后的字母为:%c,%d\n",b,b)

return 0

}