当前位置:常识百科馆>游戏数码>电脑>

C语言学习:任意大小写字母转换

电脑 阅读(2.34W)

操作方法

(01)实现任意大小写字母转换的代码:#include <stdio.h>#define DAXIE(x) (x>=&#x27;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;}

C语言学习:任意大小写字母转换

(02)利用int tolower(int ())函数,将大写字母转换成小写字母。例子:#include <ctype.h>main(){ char b[] = "qWErt222;!#$"; int i; printf("before tolower() : %bn", b); for(i = 0; i < sizeof(b); i++) b[i] = tolower(b[i]); printf("after tolower() : %bn", b);}

C语言学习:任意大小写字母转换 第2张

(03)同样,也可以利用利用int tolower(int ())函数,将小写字母转换成大写字母。例子:#include <ctype.h>main(){ char a[] = "qWErt222;!#$"; int i; printf("before toupper() : %an", a); for(i = 0; i < sizeof(a); i++) a[i] = toupper(a[i]); printf("after toupper() : %an", a);}

C语言学习:任意大小写字母转换 第3张