默认引用#include <ctype.h>头文件,可以调用tolower将字符从大写修改为小写,但有时候不想用系统带的,也可以自己实现。
下面是C语言版本的实现
int my_tolower(int c)
{
if ((c >= 'A') && (c <= 'Z'))
return c + ('a' - 'A');
return c;
}
char *strlowr(char *str)
{
char *orign = str;
for (; *str!='\0'; str++)
*str = my_tolower(*str);
return orign;
}
测试代码
int main() {
char s[] = "B2C3EE4F5G6";
printf("before strlowr():[%s]\n", s);
strlowr(s);
printf("after strlowr():[%s]\n", s);
}
运行结果
before strlowr():[B2C3EE4F5G6]
after strlowr():[b2c3ee4f5g6]