C 使输入的大写英文字母变成小写
实际上,在
<ctype.h>
头中已经有相关函数的集成tolower()
(小写变成大写是toupper()
),这里我们要自己写一个也很容易,利用acsii码的差值(很常用的方法)。
1#include <stdio.h>
2
3#define DIF ('a' - 'A')
4
5int main(void)
6{
7 char input;
8
9 while ((input = getchar()) != EOF) {
10 if (input >= 'A' && input <= 'Z') {
11 printf("%c", input + DIF);
12 } else {
13 putchar(input);
14 }
15 }
16 return 0;
17}
18
这里我们用的常量,显得更加正规一点,并且也验证了printf()
和putchar()
可以混合使用。
评论 (0)