全国计算机等级考试二级教程-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() ...
随机推荐
- Inno Setup:获取isl中的多国语言字串
原文 http://zwkufo.blog.163.com/blog/static/25882512010101041626803/?suggestedreading&wumii 用InnoS ...
- 一张图片入门Python
一张图片入门Python 之前已有别人整理了,一张图入门Python,快速了解各种基本的语法. 英文版: 图 5.1. Quick Python Script Explanation 中文版: 图 5 ...
- Go语言实现-观察者模式
前前言 这个类经过我的正式投入使用啊,发现不对劲,这样做可能会导致线程死锁 比如你dispatch一个event,然后在这个回调里把那个事件的侦听给remove掉了,那么就会导致线程死锁(这个问题找了 ...
- Asp.Net构架(Http请求处理流程)、Asp.Net 构架(Http Handler 介绍)、Asp.Net 构架(HttpModule 介绍)
转载: HttpHaddler,HttpModule http://blog.csdn.net/jiuqiyuliang/article/details/18713451 http://www.cnb ...
- Windows下安装Python3.4.2
一.Windows下安装Python3.4.2 1.下载Windows下的Python3.4.2.exe 2.指定一个目录安装,然后下一步 3.配置环境变量包括Python.exe的文件.目录如下图所 ...
- 浅析StackTrace【转】
我们在学习函数调用时,都知道每个函数都拥有自己的栈空间.一个函数被调用时,就创建一个新的栈空间.那么通过函数的嵌套调用最后就形成了一个函数调用堆栈.在c#中,使用StackTrace记录这个堆栈.你可 ...
- leetcode_question_102 Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Office 2010 & SharePoint 2010 Service Pack 2现在可用啦
Access 2010 Runtime SP2 KB2687444 32-bit 64-bit Duet Enterprise for Microsoft SharePoint and SAP SP2 ...
- <!--转换office时需要此配置 --> <identity impersonate="true" />
1.需要对Office 进行操作时 ,添加权限 <!--转换office时需要此配置 --> <identity impersonate="true" /> ...
- Java基础笔记-多线程
线程: 方式一:继承Thread类并且复写run方法. 格式: class MyThread extends Thread { public void run() { 线程中要运行的代码. } } 其 ...