操作方法
实现任意大小写字母转换的代码: #include <stdio.h> #define DAXIE(x) (x>='A' && x<='Z') //判断是大写字符。 #define XIAOXIE(x) (x>='a' && x<='z') //判断是小写字符。 #define ZHUANXIAOXIE(x) (x-'A'+'a')//转为小写 #define ZHUANDAXIE(X) (x-'a'+'A')//转为大写 int main() { char str[100]; int i; gets(str); for(i = 0; str[i]; i ++) if(DAXIE(str[i])) str[i] = ZHUANXIAOXIE(str[i]);//如果是大写字符,转为小写。 else if(XIAOXIE(str[i])) str[i] = ZHUANDAXIE(str[i]);//如果是小写字符,转为大写。 puts(str);//输出结果 return 0; }
利用int tolower(int ())函数,将大写字母转换成小写字母。 例子: #include <ctype.h> main(){ char b[] = "qWErt222;!#$"; int i; printf("before tolower() : %b\n", b); for(i = 0; i < sizeof(b); i++) b[i] = tolower(b[i]); printf("after tolower() : %b\n", b); }
同样,也可以利用利用int tolower(int ())函数,将小写字母转换成大写字母。 例子: #include <ctype.h> main(){ char a[] = "qWErt222;!#$"; int i; printf("before toupper() : %a\n", a); for(i = 0; i < sizeof(a); i++) a[i] = toupper(a[i]); printf("after toupper() : %a\n", a); }