ImageVerifierCode 换一换
格式:DOCX , 页数:12 ,大小:17.74KB ,
资源ID:5239749      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5239749.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(串的基本操作.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

串的基本操作.docx

1、串的基本操作1上机实训 3:串的基本操作一、实训目的通过实训,掌握串的运算 (赋值,比较,联结,插入子串,模式匹配 等)二、实验理论知识1)串的基本概念及其含义串( string)是由零个或多个字符组成的有限序列,一般记作 :s=a1a2 an(n0),其中 s 为串的名字,用单引号括起来的字符序列为串的值; ai(1 i n)可以是字母、数字或其它字符(取决于程序设计语言所使用的字符集); n 为串中字符的个数,称为串的长度。2)串的存储表示及其实现顺序存储可以用一组地址连续的存储单元依次存放串的各个字符,这是串的顺序存储结构,也称为顺序串链式存储和线性表的链式存储结构相类似,也可采用链表方

2、式存储串值。串的这种链式存储结构简称为链串。用链表存储字符串,每个结点需要有两个域:一个数据域( data)和一个指针域 (Next),其中数据域存放串中的字符,指针域存放后继结点的地址。3)模式匹配问题三、实训案例与分析【实例 1】串的存储与基本运算【实例分析】在本实例中练习计算字符串的长度、 字符串的复制、字符串的比较、字符串的连接、字符串的插入等基本操作。在设计时1)编写一个菜单函数,根据不同情况做( 1-5)不同选择。2)如果选择 1,即要求计算输入字符串的长度。3)如果选择 2,完成字符串的复制。4)如果选择 3,完成字符串的比较。5)如果选择 4,完成两个字符串的连接。6)如果选择

3、 5,字符串的插入。【参考程序】#include #define MAX 128typedef enum fail,success status;typedef enum false,true boolean;main() int strlen(); void strass(); boolean strcmp(); status strcat( );status strins();int t,n,i;boolean b;status st;char sMAX,s1MAX,s2MAX;printf(n1. The length of stringn);printf( 2. The assignm

4、ent of stringn);printf( 3. A string compare with another string:n); printf( 4. A string connect with another string:n); printf( 5. A string to be inserted into another stringn); printf( Please input a operation:);/*输入操作选项 */ scanf(%d,&t);switch(t)case 1:printf(please input a string:n);getchar();gets

5、(s);n=strlen(s);printf(the length is: %d,n);break;case 2:printf(please input the first string:n);getchar();gets(s1);printf(please input the second string:n);getchar();gets(s2);strass(s1,s2);break;case 3:printf(please input the first string:n);getchar();gets(s1);printf(please input the second string:

6、 n);gets(s2);b=strcmp(s1,s2);if (b=true)printf(equaln);elseprintf(not equaln);break;case 4:printf(please input the first string:n);getchar();gets(s1);printf(please input the second string:n);gets(s2);st=strcat(s1,s2);if(st=success)printf(answer is %sn,s1);elseprintf(error!n);break;case 5:printf(plea

7、se input the first string:n);getchar();gets(s1);printf(please input the second string:n);gets(s2);printf(please input i:);scanf(%d,&i);st=strins(s1,i,s2);if(st=success)printf(answer is: %sn,s1);else printf(error!n);break;case 0:break;default: printf(There isnt this operation!);int strlen(s) /* 求字符串的

8、长度子函数 */char s; int i; for(i=0;si!=0;i+); return (i);void strass(s1,s2) char s1,s2; int i=0; while(s1i!=0) s2i=s1i;i+;s2i=0;printf(s2 is %s,s2);boolean strcmp(s1,s2) /*字符串比较子函数 */char s1,s2; int i=0;while (s1i=s2i & s1i!=0 & s2i!=0)i+;if (s1i=0 & s2i=0)return (true);elsereturn (false);status strcat

9、(s1,s2) /*字符串连接子函数 */char s1,s2; int i,j,k; i=strlen(s1); j=strlen(s2); if(i+j)=MAXN) return(fail); for(k=0;k=j;k+)s1i+k=s2k;return (success);status strins (s1,i,s2)char s1,s2;int i; int m,n,k; m=strlen(s1); n=strlen(s2);if (im|(m+n)MAXN ) return (fail) ; for(k=m;k=i;k-) s1k+n=s1k; for(k=0;kn;k+) s1

10、i+k=s2k;return (success);【测试数据与结果:】计算字符串的长度1.The length of string2.The assignment of string3.A string compare with another string:4.A string connect with another string:5.A string to be inserted into another string Please input a opertation:1please input a string: you are a boy!the length is: 14字符串的

11、复制1.The length of string2.The assignment of string3.A string compare with another string:4.A string connect with another string:5.A string to be inserted into another string Please input a opertation:2please input the first string: you are a boy!please input the second string: i am a girl!s2 is you

12、are a boy!字符串的比较1.The length of string2.The assignment of string3.A string compare with another string:4.A string connect with another string:5.A string to be inserted into another stringPlease input a opertation:3please input the first string:you are a boy!please input the second string:i am a girl

13、!not equal字符串的连接1.The length of string2.The assignment of string3.A string compare with another string:4.A string connect with another string:5.A string to be inserted into another string Please input a opertation:4please input the first string: you are a boy!please input the second string: i am a g

14、irl!answer is: you are a boy!i am a girl!字符串的插入1.The length of string2.The assignment of string3.A string compare with another string:4.A string connect with another string:5.A string to be inserted into another string Please input a opertation:5please input the first string: you are a boy!please in

15、put the second string: i am a girl!please input i:2answer is i am a girl! you are a boy!【实例 2】统计主串指定单词在主串中出现的次数和位置【实例描述】统计主串指定单词在主串中出现的次数和位置,要求:1)输入以回车作为结束符的一串字符作为主串;2)求主串中指定单词出现的次数和位置,注意单词与子串的区别;【实例分析】假设 num 存放出现次数,初始化为 0,positioni 存放每一次匹配时的位置。1)编写一个菜单函数,根据不同情况做( 0-3)不同选择2)如果选择 1,即要求匹配的是单个字符 c,让 i=

16、0,依次比较主串的第 i个字符,如果 si=c ,则匹配成功, positionnum=i;num+;3)如果选择 2,即要求匹配的是子串 t,则此算法即为模式匹配算法,与模式匹配不同的是每次匹配成功时不是返回,而是设置 position 和 num 的值, positionnum=i-j;num+;4) 如果选择3,即要求匹配的是单词t,假设主串中单词之间用空格分割,此算法与 3)的不同,与 t 匹配的子串必须是一个单词, 因此首先要找出主串中的单词,然后让 t 与找到的单词比较,若结果相等,则匹配成功,设置 position 和 num 的值, positionnum=i-j; num+;

17、否则,查找主串中的下一个单词,直到主串结束。【实例实现】#include string.h#include stdio.hchar str60;void menu() /* 菜单选择函数 */char choice,c;char t20;printf( 指定类型 n);printf(=n);printf(1. 单个字符 n);printf(2. 指定子串 n);printf(3. 指定单词 n);printf(0. 退出 n);printf(=n);doprintf( 请你在上述功能中选择 (0-3) : );choice=getchar();getchar();switch(choice)c

18、ase 1:printf(input char : ); /* 指定单个字符匹配的情况 */ c=getchar();getchar();index_char(str,c);break;case 2:printf(input string : ); /* 指定子串匹配的情况 */ gets(t);index_string(str,t);break;case 3:printf(input word : ); /* 指定单词匹配的情况 */ gets(t);index_word(str,t);break;case 0:return;while(1);index_string(char *s,cha

19、r *t) /* 子串匹配函数 */int i,j,num=0,position80;/*num 存放匹配个数, position 中存放每一次匹配的位置 */ i=0;j=0;while(si!=0) /* 主串没有结束 */while(si!=0&tj!=0) /* 没有遇到结束符 */if(si=tj) /* 继续 */i+;j+;else /*回溯 */i=i-j+1;j=0;if(tj=0)positionnum=i-j;num+;j=0; /* 匹配成功,存放位置,并且次数加 1*/if(num) /* 如果匹配成功,输出匹配次数和位置 */printf(nthe number o

20、f string %s is %d,t,num);printf(nthe position is:);for(i=0;inum;i+)printf(%5d,positioni);printf(n);elseprintf(n the string is not found!,i);index_word(char *s,char *t)int i,j,num=0,position80;char word20;i=j=0;while(si!=0)j=0;while(si= )i+;while(si!= )&(si!=0) /* 寻找主串中的单词 */wordj+=stri+;wordj=0;if(s

21、trcmp(t,word)=0) /* 如果输入的单词与主串中的单词匹配 */ positionnum=i-j;num+;if(num) /* 如果匹配成功,输出匹配次数和位置 */printf(nthe number of word %s is %d,t,num);printf(nthe position is:);for(i=0;inum;i+)printf(%5d,positioni);printf(n);elseprintf(n the word is not found!);index_char(char *s,char t)int i=0,num=0,position80;whil

22、e(si!=0)if(si=t) positionnum=i;num+;i+;if(num)printf(nthe number of char %c is %d,t,num);printf(nthe position is:);for(i=0;inum;i+)printf(%5d,positioni);printf(n);elseprintf(n the char is not found!);main()printf(ninput the string1 : );gets(str);menu();【测试数据与结果】input the string1 : aab bbc ccd dde ab

23、cde指定类型=1.单个字符2.指定子串3.指定单词0.退出=请你在上述功能中选择 (0-3) : 1input char : athe number of char a is 3the position is: 0 1 16请你在上述功能中选择 (0-3) : 2input string : abthe number of string ab is 2the position is: 1 16请你在上述功能中选择 (0-3) : 3input word : ccdthe number of word ccd is 1the position is: 8请你在上述功能中选择 (0-3) : 0四、思考项目1)计算一个子串在一个字符串中出现的次数。2)统计文本文件中给定的单词数。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1