串的基本操作.docx
- 文档编号:13246247
- 上传时间:2023-06-12
- 格式:DOCX
- 页数:15
- 大小:17.55KB
串的基本操作.docx
《串的基本操作.docx》由会员分享,可在线阅读,更多相关《串的基本操作.docx(15页珍藏版)》请在冰点文库上搜索。
串的基本操作
1上机实训3:
串的基本操作
一、实训目的
通过实训,掌握串的运算(赋值,比较,联结,插入子串,模式匹配……等)
二、实验理论知识
1)串的基本概念及其含义
串(string)是由零个或多个字符组成的有限序列,一般记作:
s='a1a2…an'(n≥0),其中s为串的名字,用单引号括起来的字符序列为串的值;ai(1≤i≤n)可以是字母、数字或其它字符(取决于程序设计语言所使用的字符集);n为串中字符的个数,称为串的长度。
2)串的存储表示及其实现
●顺序存储
可以用一组地址连续的存储单元依次存放串的各个字符,这是串的顺序存储结构,也称为顺序串
●链式存储
和线性表的链式存储结构相类似,也可采用链表方式存储串值。
串的这种链式存储结构简称为链串。
用链表存储字符串,每个结点需要有两个域:
一个数据域(data)和一个指针域(Next),其中数据域存放串中的字符,指针域存放后继结点的地址。
3)模式匹配问题
三、实训案例与分析
【实例1】串的存储与基本运算
【实例分析】在本实例中练习计算字符串的长度、字符串的复制、字符串的比较、字符串的连接、字符串的插入等基本操作。
在设计时
1)编写一个菜单函数,根据不同情况做(1-5)不同选择。
2)如果选择1,即要求计算输入字符串的长度。
3)如果选择2,完成字符串的复制。
4)如果选择3,完成字符串的比较。
5)如果选择4,完成两个字符串的连接。
6)如果选择5,字符串的插入。
【参考程序】
#include
#defineMAX128
typedefenum{fail,success}status;
typedefenum{false,true}boolean;
main()
{intstrlen();
voidstrass();
booleanstrcmp();
statusstrcat();
statusstrins();
intt,n,i;
booleanb;
statusst;
chars[MAX],s1[MAX],s2[MAX];
printf("\n1.Thelengthofstring\n");
printf("2.Theassignmentofstring\n");
printf("3.Astringcomparewithanotherstring:
\n");
printf("4.Astringconnectwithanotherstring:
\n");
printf("5.Astringtobeinsertedintoanotherstring\n");
printf("Pleaseinputaoperation:
");/*输入操作选项*/
scanf("%d",&t);
switch(t)
{
case1:
printf("pleaseinputastring:
\n");
getchar();
gets(s);
n=strlen(s);
printf("thelengthis:
%d",n);
break;
case2:
printf("pleaseinputthefirststring:
\n");
getchar();
gets(s1);
printf("pleaseinputthesecondstring:
\n");
getchar();
gets(s2);
strass(s1,s2);
break;
case3:
printf("pleaseinputthefirststring:
\n");
getchar();
gets(s1);
printf("pleaseinputthesecondstring:
\n");
gets(s2);
b=strcmp(s1,s2);
if(b==true)
printf("equal\n");
else
printf("notequal\n");
break;
case4:
printf("pleaseinputthefirststring:
\n");
getchar();
gets(s1);
printf("pleaseinputthesecondstring:
\n");
gets(s2);
st=strcat(s1,s2);
if(st==success)
printf("answeris%s\n",s1);
else
printf("error!
\n");
break;
case5:
printf("pleaseinputthefirststring:
\n");
getchar();
gets(s1);
printf("pleaseinputthesecondstring:
\n");
gets(s2);
printf("pleaseinputi:
");
scanf("%d",&i);
st=strins(s1,i,s2);
if(st==success)
printf("answeris:
%s\n",s1);
elseprintf("error!
\n");
break;
case0:
break;
default:
printf("Thereisn'tthisoperation!
");
}
}
intstrlen(s)/*求字符串的长度子函数*/
chars[];
{inti;
for(i=0;s[i]!
='\0';i++);
return(i);
}
voidstrass(s1,s2)
chars1[],s2[];
{inti=0;
while(s1[i]!
='\0')
{s2[i]=s1[i];
i++;
}
s2[i]='\0';
printf("s2is%s",s2);
}
booleanstrcmp(s1,s2)/*字符串比较子函数*/
chars1[],s2[];
{inti=0;
while(s1[i]==s2[i]&&s1[i]!
='\0'&&s2[i]!
='\0')
i++;
if(s1[i]=='\0'&&s2[i]=='\0')
return(true);
else
return(false);
}
statusstrcat(s1,s2)/*字符串连接子函数*/
chars1[],s2[];
{inti,j,k;
i=strlen(s1);
j=strlen(s2);
if((i+j)>=MAXN)
return(fail);
for(k=0;k<=j;k++)
s1[i+k]=s2[k];
return(success);
}
statusstrins(s1,i,s2)
chars1[],s2[];
inti;
{intm,n,k;
m=strlen(s1);
n=strlen(s2);
if(i<0||i>m||(m+n)>MAXN)
return(fail);
for(k=m;k>=i;k--)
s1[k+n]=s1[k];
for(k=0;k s1[i+k]=s2[k]; return(success); } 【测试数据与结果: 】 计算字符串的长度 1.Thelengthofstring 2.Theassignmentofstring 3.Astringcomparewithanotherstring: 4.Astringconnectwithanotherstring: 5.Astringtobeinsertedintoanotherstring Pleaseinputaopertation: 1 pleaseinputastring: youareaboy! thelengthis: 14 字符串的复制 1.Thelengthofstring 2.Theassignmentofstring 3.Astringcomparewithanotherstring: 4.Astringconnectwithanotherstring: 5.Astringtobeinsertedintoanotherstring Pleaseinputaopertation: 2 pleaseinputthefirststring: youareaboy! pleaseinputthesecondstring: iamagirl! s2isyouareaboy! 字符串的比较 1.Thelengthofstring 2.Theassignmentofstring 3.Astringcomparewithanotherstring: 4.Astringconnectwithanotherstring: 5.Astringtobeinsertedintoanotherstring Pleaseinputaopertation: 3 pleaseinputthefirststring: youareaboy! pleaseinputthesecondstring: iamagirl! notequal 字符串的连接 1.Thelengthofstring 2.Theassignmentofstring 3.Astringcomparewithanotherstring: 4.Astringconnectwithanotherstring: 5.Astringtobeinsertedintoanotherstring Pleaseinputaopertation: 4 pleaseinputthefirststring: youareaboy! pleaseinputthesecondstring: iamagirl! answeris: youareaboy! iamagirl! 字符串的插入 1.Thelengthofstring 2.Theassignmentofstring 3.Astringcomparewithanotherstring: 4.Astringconnectwithanotherstring: 5.Astringtobeinsertedintoanotherstring Pleaseinputaopertation: 5 pleaseinputthefirststring: youareaboy! pleaseinputthesecondstring: iamagirl! pleaseinputi: 2 answerisiamagirl! youareaboy! 【实例2】统计主串指定单词在主串中出现的次数和位置 【实例描述】统计主串指定单词在主串中出现的次数和位置,要求: 1)输入以回车作为结束符的一串字符作为主串; 2)求主串中指定单词出现的次数和位置,注意单词与子串的区别; 【实例分析】 假设num存放出现次数,初始化为0,position[i]存放每一次匹配时的位置。 1)编写一个菜单函数,根据不同情况做(0-3)不同选择 2)如果选择1,即要求匹配的是单个字符c,让i=0,依次比较主串的第i个字符,如果s[i]==c,则匹配成功,position[num]=i;num++; 3)如果选择2,即要求匹配的是子串t,则此算法即为模式匹配算法,与模式匹配不同的是每次匹配成功时不是返回,而是设置position和num的值,position[num]=i-j;num++; 4)如果选择3,即要求匹配的是单词t,假设主串中单词之间用空格分割,此算法与3)的不同,与t匹配的子串必须是一个单词,因此首先要找出主串中的单词,然后让t与找到的单词比较,若结果相等,则匹配成功,设置position和num的值,position[num]=i-j;num++;否则,查找主串中的下一个单词,直到主串结束。 【实例实现】 #include"string.h" #include"stdio.h" charstr[60]; voidmenu()/*菜单选择函数*/ {charchoice,c; chart[20]; printf("指定类型\n"); printf("====================\n"); printf("1.单个字符\n"); printf("2.指定子串\n"); printf("3.指定单词\n"); printf("0.退出\n"); printf("====================\n"); do { printf("请你在上述功能中选择(0-3): "); choice=getchar(); getchar(); switch(choice) {case'1': printf("inputchar: ");/*指定单个字符匹配的情况*/ c=getchar();getchar(); index_char(str,c); break; case'2': printf("inputstring: ");/*指定子串匹配的情况*/ gets(t); index_string(str,t);break; case'3': printf("inputword: ");/*指定单词匹配的情况*/ gets(t); index_word(str,t);break; case'0': return; } }while (1); } index_string(char*s,char*t)/*子串匹配函数*/ {inti,j,num=0,position[80]; /*num存放匹配个数,position中存放每一次匹配的位置*/ i=0;j=0; while(s[i]! ='\0')/*主串没有结束*/ {while(s[i]! ='\0'&&t[j]! ='\0')/*没有遇到结束符*/ if(s[i]==t[j])/*继续*/ {i++; j++;} else/*回溯*/ {i=i-j+1; j=0; } if(t[j]=='\0') {position[num]=i-j;num++;j=0;}/*匹配成功,存放位置,并且次数加1*/ } if(num)/*如果匹配成功,输出匹配次数和位置*/ {printf("\nthenumberofstring\"%s\"is%d",t,num); printf("\nthepositionis: "); for(i=0;i printf("%5d",position[i]); printf("\n");} else printf("\nthestringisnotfound! ",i); } index_word(char*s,char*t) {inti,j,num=0,position[80]; charword[20]; i=j=0; while(s[i]! ='\0') {j=0; while(s[i]=='') i++; while((s[i]! ='')&&(s[i]! ='\0'))/*寻找主串中的单词*/ word[j++]=str[i++]; word[j]='\0'; if(strcmp(t,word)==0)/*如果输入的单词与主串中的单词匹配*/ {position[num]=i-j;num++;} } if(num)/*如果匹配成功,输出匹配次数和位置*/ { printf("\nthenumberofword\"%s\"is%d",t,num); printf("\nthepositionis: "); for(i=0;i printf("%5d",position[i]); printf("\n"); } else printf("\nthewordisnotfound! "); } index_char(char*s,chart) { inti=0,num=0,position[80]; while(s[i]! ='\0') {if(s[i]==t){position[num]=i;num++;} i++;} if(num) { printf("\nthenumberofchar'%c'is%d",t,num); printf("\nthepositionis: "); for(i=0;i printf("%5d",position[i]); printf("\n"); } else printf("\nthecharisnotfound! "); } main() {printf("\ninputthestring1: "); gets(str); menu(); } 【测试数据与结果】 inputthestring1: aabbbcccdddeabcde 指定类型 ==================== 1.单个字符 2.指定子串 3.指定单词 0.退出 ==================== 请你在上述功能中选择(0-3): 1 inputchar: a thenumberofchar'a'is3 thepositionis: 0116 请你在上述功能中选择(0-3): 2 inputstring: ab thenumberofstring"ab"is2 thepositionis: 116 请你在上述功能中选择(0-3): 3 inputword: ccd thenumberofword"ccd"is1 thepositionis: 8 请你在上述功能中选择(0-3): 0 四、思考项目 1)计算一个子串在一个字符串中出现的次数。 2)统计文本文件中给定的单词数。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基本 操作