全国计算机等级考试二级教程-C语言程序设计_第6章_字符型数据
#include <stdio.h>
main()
{
char c;
char d;
c = ;
d = ''; if (c == d)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
字符必须用单引号
#include <stdio.h>
main()
{
char ch = 'A'; /*字符必须用单引号*/
}
逃逸字符
用来表达无法印出来的控制字符或特殊字符,它由一个反斜杠\开头,后面跟上另一个字符,这两个字符合起来,组成了一个字符。
#include <stdio.h>
main()
{
printf("请分别输入身高的英尺和英寸,""如输入\"5 7\"表示5英尺7英寸:");
}
输出结果:
请分别输入身高的英尺和英寸,如输入"5 7"表示5英尺7英寸:请按任意键继续. . .
6.1 一个整数,只要它的值在0-256范围内,也可以用字符形式输出;反之,一个字符型数据也可以用整数形式输出。
#include <stdio.h>
main()
{
char c = 'a';
int i = ; printf("%c,%d\n", c, c);
printf("%c,%d\n", i, i);
}
输出格式:
a,97
a,97
请按任意键继续. . .
6.2 大、小写字母转换
#include <stdio.h>
main()
{
char c1, c2;
c1 = 'a';
c2 = 'b'; c1 = c1 - ;
c2 = c2 - ; printf("%c %c\n", c1, c2);
}
输出格式:
A B
请按任意键继续. . .
6.3 以下程序输出26个大写字母和它们的ASCII代码,每行输出两组数据
#include <stdio.h>
main()
{
char ch;
int i; for (i = ;i < ;i++)
{
ch = i + ;
if (i % == )
printf("\n");
printf(" c=%c,ASCIID=%d", ch, ch);
}
putchar('\n');
}
6.4 以下程序段等待从终端输入一个字符,当按Enter键时,程序才往下继续进行
#include <stdio.h>
int main(void)
{ while (getchar() != '\n'); }
6.5 以下程序把从终端输入的一行字符中所有的小写字母转换成大写字母,其他字符不变
#include <stdio.h>
main()
{
char c; while ((c = getchar()) != '\n')
{
if (c >= 'a'&&c <= 'z')
c = c - ;
putchar(c);
}
putchar('\n');
}
6.6 编写程序统计输入的字符中空格符、换行符和横向跳格(制表)符的个数,用 ! 号结束输入
#include <stdio.h>
#include <ctype.h>
main()
{
long n = ;
char ch; while ((ch = getchar()) != '!')
{
if (isspace(ch))
n++;
}
printf("n=%ld\n", n);
}
6.7 请编写程序,输入一行字符(用回车结束),输出每个字符以及与之对应的ASCII代码值,每行输出三对。
#include <stdio.h>
main()
{
char ch;
int n = ; while ((ch = getchar()) != '\n')
{
if (n % == )
{
putchar('\n');
}
++n;
printf("%c:%d,", ch, ch);
}
}
6.8 请编写程序,输入一行数字字符(用回车结束),每个数字字符的前后都有空格。请编程,把这一行中的数字转换成一个整数。例如,若输入(<CR>代表Enter键):
2 4 8 3 <CR>
则输出整数:2483.
#include <stdio.h>
#include <ctype.h>
main()
{
char ch; while ((ch = getchar()) != '\n')
{
if (isdigit(ch))
printf("%c", ch);
}
putchar('\n');
}
6.9 请编写程序统计输入的行数,用 ! 号结束输入, ! 号所在行不计入行数。
#include <stdio.h>
main()
{
char ch;
int n = ; while ((ch = getchar()) != '!') //第1次,必须使用ch = getchar(),因为ch需要初始化
{
if (ch == '\n') //第2次,可用可不用
{
++n;
}
}
printf("%d", n);
}
6.10 请编写程序统计输入的一行中小写字母的个数。
#include <stdio.h>
#include <ctype.h>
main()
{
char ch;
int n = ; while ((ch = getchar()) != '\n')
{
if (islower(ch))
{
++n;
}
}
printf("%d\n", n);
}
全国计算机等级考试二级教程-C语言程序设计_第6章_字符型数据的更多相关文章
- 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串
字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...
- 全国计算机等级考试二级教程-C语言程序设计_第4章_选择结构
switch什么时候用break,什么时候不用break 调用break:一次执行一个分支,输入一个数据,对应一个级别 不调用break:连续执行多个分支 if...else 可以处理任何情况,大于小 ...
- 全国计算机等级考试二级教程-C语言程序设计_第8章_地址和指针
面试: unsigned int *p1 = # int *p2 = # #define _CRT_SECURE_NO_WARNINGS #include<std ...
- 全国计算机等级考试二级教程-C语言程序设计_第15章_位运算
位运算,不适用于实数,仅仅适用于整数.字符. C语言的位运算只能操作整数.字符,实数是指数方式表示的,不适用于位运算. #define _CRT_SECURE_NO_WARNINGS #include ...
- 全国计算机等级考试二级教程-C语言程序设计_第9章_数组
四维数组,可用于航天卫星,三维+时间 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
- 全国计算机等级考试二级教程-C语言程序设计_第14章_结构体、共用体和用户定义类型
函数的返回值是结构体类型 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct ...
- 全国计算机等级考试二级教程-C语言程序设计_第5章_循环结构
for循环结构的嵌套 外层循环每循环一次,内层循环会完整循环一次. 外层循环是竖. 内层循环是横. for, do...while, while的选择: 如果有固定次数,如阶乘! ,判断素数,用 fo ...
- 全国计算机等级考试二级教程-C语言程序设计_第3章_顺序结构
1输入两个整数给变量x和y:然后输出x和y:在交换x和y中的值后,在输出x和y. #include <stdio.h> main() { int x, y, t; printf(" ...
- 全国计算机等级考试二级教程-C语言程序设计_第2章_C程序设计的初步知识
正负号与被除数一致. 3 % (-5) == 3 (-3) % 5 == -3 不用求余运算符,求出余数. int x, y; 答:x - x / y * y; const int i = 10; c ...
- 全国计算机等级考试二级教程-C语言程序设计_第7章_函数
函数执行,从右到左执行 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> main() ...
随机推荐
- 【额 原来ms sqlserver 中的视图果然是“虚表”哈】
在视图中查询数据的时候,会不会使用实体表中的列上的索引呢?会 .... 测试结果 测试脚本 ; BEGIN INSERT INTO Teachers ( TeacherName, Sex, Money ...
- 使用高性能xml序列化框架jibx作为spring mvc的xml view
package org.springframework.web.servlet.view.xml; import java.io.ByteArrayOutputStream; import java. ...
- 10453 Make Palindrome (dp)
Problem A Make Palindrome Input: standard input Output: standard output Time Limit: 8 seconds By def ...
- android 拍照 onCreate() 调用两次的问题
拍照的代码网上都有就不写了!自己找下就ok了! 1 旋转屏幕导致问题! 这种情况很好解决:在androidManifest.xml 中设置activity 添加属性 android:configC ...
- lucas模板
ll PowMod(ll a,ll b,ll MOD){ ll ret=; while(b){ ) ret=(ret*a)%MOD; a=(a*a)%MOD; b>>=; } return ...
- Material Design之FloatingActionButton的使用
FloatingActionButton是继承至ImageView,所以FloatingActionButton拥有ImageView的全部属性. CoordinatorLayout能够用来配合Flo ...
- ab -n -c
ab是apache自带的一个很好用的压力测试工具,当安装完apache的时候,就可以在bin下面找到ab 1 我们可以模拟100个并发用户,对一个页面发送1000个请求 ./ab -n1000 -c1 ...
- VMware虚拟机中调整Linux分区大小手记(转发)
前段时间用VMware5.5安装了CentOS5.3,安装的时候分配了5Gb的虚拟硬盘空间给Linux系统,系统安装选择很多组件和软件,后面使用时又安装也一些软件,结果导致虚拟硬盘空间不足.查看分 ...
- centos yum 完全卸载依赖
centos yum 完全卸载依赖 you install a package with yum install, say pdftk, it will pull in a lot of dep ...
- Permutations,Permutations II,Combinations
这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...