TP9字符串.ppt





《TP9字符串.ppt》由会员分享,可在线阅读,更多相关《TP9字符串.ppt(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、第九章第九章字符串目标o理解字符串常量o理解字符数组和字符串的异同o使用字符串输入/输出函数o使用字符串处理函数2字符串简介char a=W;char b=a;char c=n;char d=g;char e=L;char f=i;如何让程序存储我的姓名?Wang Lichar name15=W,a,n,g,L,i;用若干个字符用字符数组用字符串3字符串常量1-1o字符串常量是双引号括起的任意字符序列Hello WorldWangPingPlease enter your full name:Hello Accp字符串常量中可以包含转义序列HelloWorld0字符串结束符4字符串与字符数组1
2、-2o在语言中没有专门的字符串变量,通常用一个字符数组来存放一个字符串o字符数组和字符串的区别是:字符串的末尾有一个空字符 0字符串可按如下方式声明并初始化:char name15=W,a,n,g,L,i,0;char name15=WangLi;char password=12345678;手工加入一个空字符系统将自动加入一个空字符省略数组大小,系统自动计算,大小为后面的字符总数加1,最后一个元素存入一个空字符。字符数组赋字符串 课本例25指向字符串的指针2-1o在C语言中,可以将字符指针指向字符串o被指向的字符串可以是一个常量字符串,也可以是一个存储着字符串的字符数组char*ptr=C
3、Language;CLanguage0ptrchar*ps=We change lives;int n=10;ps=ps+n;printf(%sn,ps);W echangelives0ps lives6指向字符串的指针char uname30=wahaha yiyiyaya;char*p;int count;count=0;for(p=uname;*p!=0;p+)if(*p=a)count+;W ahahayiyiyaya0psunamecount0 5循环7字符串I/0函数3-2char name10;scanf(%s,name);printf(%s,name);格式描述串中使用转换字符
4、串“%s”Wang LiWang0使用 scanf 时,不能输入空格Wang8字符串I/0函数3-3char name10;gets(name);puts(name);W angLi0Wang LiWang Li从键盘上读入一个完整的行,存入字符数组name。并用空字符0取代行尾的换行符n。把字符数组中的字符串输出到显示器。使用 gets函数允许输入空格9字符串I/0函数puts 函数完全可以由 printf 函数取代。当需要按一定格式输出时,通常使用 printf 函数。printf(n 雇员姓名是:);puts(name);printf(n 雇员所属部门是:);puts(dept);pri
5、ntf(n 雇员姓名是:%s,name);printf(n 雇员所属部门是:%sn,dept);使用puts函数输出字符串使用printf函数输出字符串10字符串示例#include void main()char line30;int i,count=0;printf(n 请输入一行字符:n);gets(line);i=0;while(linei!=0)if(linei=)count+;i+;printf(“n 其中的空格总数为%d n,count);C is a programming language在循环执行时,扫描整个数组以统计出空格 的数量,直到遇到字符 0。每次循环执行时,都会更
6、新计数器 i 和 count。其中的空格总数为 411字符指针数组4-2o字符指针数组:一个数组中的各个元素都是字符指针char*names =Apple,Pear,Peach,Banana;Apple0Pear0Banana0Peach0EE05EE20EE44EE87names各字符串的首地址二维数组与字符串数组 char name420=“I”,”am”,”a”,”doctor”;12字符指针数组示例void main()char*names=Apple,Banana,Pineapple,Peach,Strawberry,Grapes;char*temp;printf(“n%s%s,na
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- TP9 字符串

限制150内