全国计算机等级考试二级教程-C语言程序设计_第6章_字符型数据


 1 #include <stdio.h>
 2 main()
 3 {
 4     char c;
 5     char d;
 6     c = 1;
 7     d = '1';
 8 
 9     if (c == d)
10     {
11         printf("yes
");
12     }
13     else
14     {
15         printf("no
");
16     }
17 }

字符必须用单引号

1 #include <stdio.h>
2 main()
3 {
4     char ch = 'A';        /*字符必须用单引号*/
5 }

 

逃逸字符

用来表达无法印出来的控制字符或特殊字符,它由一个反斜杠开头,后面跟上另一个字符,这两个字符合起来,组成了一个字符。

1 #include <stdio.h>
2 main()
3 {
4     printf("请分别输入身高的英尺和英寸,""如输入"5 7"表示5英尺7英寸:");
5 }

输出结果:

请分别输入身高的英尺和英寸,如输入"5 7"表示5英尺7英寸:请按任意键继续. . .

6.1 一个整数,只要它的值在0-256范围内,也可以用字符形式输出;反之,一个字符型数据也可以用整数形式输出。

1 #include <stdio.h>
2 main()
3 {
4     char c = 'a';
5     int i = 97;
6 
7     printf("%c,%d
", c, c);
8     printf("%c,%d
", i, i);
9 }


输出格式:

a,97
a,97
请按任意键继续. . .

6.2 大、小写字母转换

 1 #include <stdio.h>
 2 main()
 3 {
 4     char c1, c2;
 5     c1 = 'a';
 6     c2 = 'b';
 7 
 8     c1 = c1 - 32;
 9     c2 = c2 - 32;
10 
11     printf("%c %c
", c1, c2);
12 }

输出格式:

A B
请按任意键继续. . .

6.3 以下程序输出26个大写字母和它们的ASCII代码,每行输出两组数据

 1 #include <stdio.h>
 2 main()
 3 {
 4     char ch;
 5     int i;
 6 
 7     for (i = 0;i < 26;i++)
 8     {
 9         ch = i + 65;
10         if (i % 2 == 0)
11             printf("
");
12         printf(" c=%c,ASCIID=%d", ch, ch);
13     }
14     putchar('
');
15 }

 

6.4 以下程序段等待从终端输入一个字符,当按Enter键时,程序才往下继续进行

1 #include <stdio.h>
2 int main(void)
3 {
4     
5     while (getchar() != '
');
6 
7 }

6.5 以下程序把从终端输入的一行字符中所有的小写字母转换成大写字母,其他字符不变

 1 #include <stdio.h>
 2 main()
 3 {
 4     char c;
 5 
 6     while ((c = getchar()) != '
')
 7     {
 8         if (c >= 'a'&&c <= 'z')
 9             c = c - 32;
10         putchar(c);
11     }
12     putchar('
');
13 }

6.6 编写程序统计输入的字符中空格符、换行符和横向跳格(制表)符的个数,用 ! 号结束输入

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 main()
 4 {
 5     long n = 0;
 6     char ch;
 7 
 8     while ((ch = getchar()) != '!')
 9     {
10         if (isspace(ch))
11             n++;
12     }
13     printf("n=%ld
", n);
14 }

 

6.7 请编写程序,输入一行字符(用回车结束),输出每个字符以及与之对应的ASCII代码值,每行输出三对。

 1 #include <stdio.h>
 2 main()
 3 {
 4     char ch;
 5     int n = 0;
 6 
 7     while ((ch = getchar()) != '
')
 8     {
 9         if (n % 3 == 0)
10         {
11             putchar('
');
12         }
13         ++n;
14         printf("%c:%d,", ch, ch);
15     }
16 }

 

6.8 请编写程序,输入一行数字字符(用回车结束),每个数字字符的前后都有空格。请编程,把这一行中的数字转换成一个整数。例如,若输入(<CR>代表Enter键):

 2 4 8 3 <CR>

则输出整数:2483.

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 main()
 4 {
 5     char ch;
 6 
 7     while ((ch = getchar()) != '
')
 8     {
 9         if (isdigit(ch))
10             printf("%c", ch);
11     }
12     putchar('
');
13 }

 

6.9 请编写程序统计输入的行数,用 ! 号结束输入, ! 号所在行不计入行数。

 1 #include <stdio.h>
 2 main()
 3 {
 4     char ch;
 5     int n = 0;
 6 
 7     while ((ch = getchar()) != '!')        //第1次,必须使用ch = getchar(),因为ch需要初始化
 8     {
 9         if (ch == '
')        //第2次,可用可不用
10         {
11             ++n;
12         }
13     }
14     printf("%d", n);
15 }

 6.10 请编写程序统计输入的一行中小写字母的个数。

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 main()
 4 {
 5     char ch;
 6     int n = 0;
 7 
 8     while ((ch = getchar()) != '
')
 9     {
10         if (islower(ch))
11         {
12             ++n;
13         }
14     }
15     printf("%d
", n);
16 }
原文地址:https://www.cnblogs.com/denggelin/p/5379878.html