int main()
{
int a = toupper(97);//97对应小写字母a,有对应的大写字母A,返回对应大写字母的ascll值65
int b = toupper(65);//65对应的大写字母A,无需转换,返回65
printf("%d %d\n", a, b);
printf("%c %c\n", a, b);
return 0;
}
运行结果为:
也可以这样:
c复制代码
#include <stdio.h>
#include <ctype.h>
int main()
{
char a = (char)toupper('a');
printf("%d %c", a, a);
return 0;
}
这里的 ' a '在使用的时候本质时传了对应的ascll码,返回值为int
所以将其强制转换为char类型之后再存到变量a中
1.2 tolower() 大写转小写
1.2.1 等效函数
c复制代码
#include <stdio.h>
#include <string.h>
int my_tolower(int c)
{
if ('A' <= c && c <= 'Z')
{
return c + 32;
}//当c为大写字母的ascll的值时,返回对应小写字母的ascll值
else
{
return c;
}//否则返回参数c
}
int main()
{
char str[20] = "ABCDEF#abcdef";
int sz = strlen(str);
for (int i = 0; i < sz; i++)
{
if ('A' <= str[i] && str[i] <= 'Z')
{
str[i] = my_tolower(str[i]);
}
}
printf("%s\n", str);
return 0;
}