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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

实验五教案含答案.docx

1、实验五教案含答案第五课教学内容:一维字符数组删除及统计类算法教学目的:掌握一维字符数组删除及统计类算法的考题教学内容:一、一维字符数组删除及统计类算法的考题特点此类算法多以填空题和程序设计题为考试形式。常见考点1、在一维字符数组中删除指定的单个字符,删除符合某条件的多个字符。2、统计一维字符数组中某个字符出现的次数,子串出现的次数。二、课堂练习第一套1、程序填空题下列给定程序中,函数 fun()的功能是:从字符串 s 中,删除所有小写字母c。 试题程序: _1_ /# include void fun(char *s) int i, j; for(i=j=0;_2_;i+) /si!= 0 i

2、f(si!= c) _3_; /sj+=si; sj=0; main() char s80; printf(nEnter a string: ); gets(s); printf(The original string: ); puts(s); fun(s); printf(The string after deleted: ); puts(s); printf(nn ); 2、程序改错题给定程序 modi.c 中函数 fun 的功能是:删除字符串 s 中的所有空白字符(包括 Tab 字符、回车符及换行符)。 输入字符串时用#结束输入。 请改正程序中的错误,使它能输出正确的结果。 注意:不要改

3、动 main 函数, #include #include #include fun ( char *p) int i,t; char c80; /*found*/ For (i = 0,t = 0; pi ; i+) /for if(!isspace(*(p+i) ct+=pi; /*found*/ ct=0; / ct= 0; strcpy(p,c); main( ) char c,s80; int i=0; printf(Input a string:); c=getchar(); while(c!=#) si=c;i+;c=getchar(); si=0; fun(s); puts(s)

4、; 3、程序设计题1)规定输入的字符串中只包含字母和*号。请编写函数 fun,它的功能是:使字符串最前面连续的*号不得多于 n 个;若多于 n 个,则删除多余的*号;若少于或等于 n 个,则什么也不做,字符串中间和尾部的*号不删除。例如,字符串中的内容为:*A*BC*DEF*G*,若 n 的值为 4,删除后,字符串中的内容应当是:*A*BC*DEF*G*;若 n 的值为 8,则字符串中的内容仍为:*A*BC*DEF*G*。n 的值在主函数中输入。在编写函数时,不得使用C 语言提供的字符串函数。 注意:部分源程序存在文件 prog.c 中。 请勿改动主函数 main 和其他函数中的任何内容,仅在

5、函数 fun 的花括号中填入你编写的若干语句。 #include #include void fun(char *a, int n) int i,j,k=0; for(i=0;ai=*;i+); if(in) for(j=i-n;aj!=0;j+) ak+=aj; ak=0; main() char s81;int n; printf(Enter a string:n); gets(s); printf(Enter n:); scanf(%d,&n); fun(s,n); printf(The string after deleted:n); puts(s); 2)函数 fun 的功能是:将

6、s 所指字符串中下标为偶数同时 ASCII 值也为偶数的字符删除, s 中剩余的字符形成一个新串放在 t 所指的数组中。 例如,若 s 所指字符串中的内容为ABCDEFG12345,其中字符 B 的 ASCII 码值虽为偶数,但在数组中的下标为奇数,因此不能删除;而字符 2 的 ASCII 码值为偶数, 在数组中的下标也为偶数,因此应当删除,其它依此类推。 最后 t 所指的数组中的内容应是ABCDEFG135。 注意: 部分源程序存在文件 prog.c 中。请勿改动主函数 main 和其它函数中的任何内容,在函数 fun 的花括号中填入你编写的若干语句。 #include #include #

7、include void fun(char *s, char t) int i,j=0; for(i=0;si!=0;i+) if( ! (i%2=0 & si%2=0) ) tj+=si; tj=0; main() char s100, t100; printf(nPlease enter string S:); scanf(%s, s); fun(s, t); printf(nThe result is: %sn, t); 第二套1、程序填空题(找子串)下列给定程序中,函数 fun()的功能是:计算 s 所指字符串中含有 t 所指字符串的数目,并作为函数值返回。 #include #inc

8、lude #include #define N 80 int fun(char *s,char *t) int n; char *p, *r; n=0; while(*s) p=s; r=t; while(*r) if(*r=*p) r+; _1_; /p+ else break; if(*r=_2_) /0 n+; _3_; / s+ return n; main() char aN,bN; int m; printf(nPlease enter string a: );gets(a); printf(nPlease enter substring b: ); gets(b); m=fun(

9、a,b); printf(nThe result is :m=%dn,m); 2、程序改错题文件 MODI.C 中程序的功能是:统计一个英文句子中各小写的元音字母出现的次数。 例如,若输入字符串: I am glad when playing games,则应输出: a 4 e 2 i 1 o 0 u 0 。注意:请改正程序中的错误,使它能计算出正确的结果。 #include #include int vv26; void Vs( char* s ) for( ; *s; s+ ) /*found*/ switch( s ); / switch( *s ) case( a ): case( e

10、 ): case( i ): case( o ): case( u ): vv *s - a +; main() char str81, ww6 = aeiou ; int i; printf( nPlease enter a string:n ); gets( str ); Vs( str ); /*found*/ for( i = 0; i 6; i+ ) / for( i = 0; i %2d, wwi, vv wwi - ww0 ); getch(); 3、程序设计题1)请编写函数 fun,函数的功能是:在字符串中的所有数字字符前加一个$字符。例如,输入A1B23CD45,则输出为:A

11、$1B$2$3CD$4$5。 注意:部分源程序存在文件 prog.c 中。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 #include void fun(char *s) int i,j=0,t80; for(i=0;si!=0;i+) if(si=0&si=9) tj+=$; tj+=si; else tj+=si; tj=0; for(i=0;ti!=0;i+) si=ti; si=0; main() char s80; printf(Enter a string:); scanf(%s, s); fun(s); printf(The re

12、sult:%sn, s); 2)请编写一个函数 void fun(char *tt, int pp),统计在 tt 字符串中 a 到 z 26 个字母各自出现的次数,并由 pp 数组传回 main 函数。 运行时输入字符串:abcdefgabcdeabc 后,并且输出的结果应该是:3 3 3 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 注意:部分源程序存在文件 prog.c 中。 请勿改动主函数 main 和其它函数中的任何内容,在函数 fun 的花括号中填入你编写的若干语句。 #include #include void fun(char *t

13、t, int pp) int i; for(i=0;i=a&tti=z) pp tti-a +; main( ) char aa1000 ; int bb26, k, n ; printf( nPlease enter a char string: ) ; scanf(%s, aa) ; fun(aa, bb ) ; for ( k = 0 ; k 26 ; k+ ) printf (%d , bbk) ; printf( n ) ; 三、课后练习1、程序填空题函数 char * MyDelete( char * str, char c )的功能是:在字符串 str中删除变量 c 中的字符,有

14、几个删几个,并返回所删字符的个数。 例如,若输入字符串ab abc bcdef 和 字符b,则应输出字符串a ac cdef 和 3(表示删了 3 个字符 b)。 注意:此程序存在 prog.c 中。 请勿改动主程序 main、函数 WriteData 和函数 compute 中的任何内容,仅在函数MyDelete 中的横线处填入适当内容并删除横线。 #include #include int MyDelete( char* str, char c ) int i, j=0, k=0; for( i = 0; stri; i+ ) if( stri != c ) str j = stri; _

15、(1)_; / j+ else k+; strj=_(2)_ ; /0 return( _(3)_ ); /k main() char string81, x; printf(nPlease enter a string:n); gets(string); printf(nPlease enter a character: ); scanf(%c, &x ); printf( nnAfter deleting %d %c(s), the string becomes:n%sn,MyDelete(string, x), x, string); 2、程序填空题下列给定程序中,函数 fun()的功能

16、是:删除字符串 s 中所有空白字符(包括 Tab 字符、回车符及换行符)。输入字符串时用#结束输入。 #include #include #include fun(char *p) int i, t; char c80; for(i=0,t=0;pi;i+) if(!isspace(*(p+i) _1_; /ct+=pi ct=0; strcpy(p,c); main() char c,s80; int i=0; printf(Input a string: ); c=getchar(); while(_2_) /c!=# si=c;i+;c=getchar(); _3_; /si=0 fun

17、(s); puts(s); 3、程序填空题请补充main函数,该函数的功能是:从一个字符串中截取前面若干个给定长度的子字符串。其中,str1指向原字符串,截取后的字符存放在str2所指的字符数组中,n中存放需截取的字符个数。例如:当str1=“cdefghij”,然后输入4,则str2=“cdef”。 注意:部分源程序给出如下。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun()的横线上填入所编写的若干表达式或语句。 #include #include #define LEN 80 main() char str1LEN,str2LEN; int n,i; printf(E

18、nter the string:n); gets(str1); printf(Enter the position of the string deleted:); scanf(【1】); / %d,&n for(i=0;i n;i+) 【2】 / str2i=str1i ; str2i=0; printf(The new string is:%sn,【3】); / str2 4、程序填空题给定程序的功能是判断字符 ch 是否与串 str 中的某个字符相同;若相同,什么也不做,若不同,则插在串的最后。 0123456789ABCDEch=Dch=K#include #include void

19、fun(char *str, char ch ) while ( *str & *str != ch ) str+; /*found*/ if ( *str _1_ ch ) / != str 0 = ch; /*found*/ _2_ = 0; / str1 main( ) char s81, c ; printf( nPlease enter a string:n ); gets ( s ); printf (n Please enter the character to search : ); c = getchar(); /*found*/ fun(_3_) ; / s,c 调用函数填

20、实参,需要参考对应的形参 printf( nThe result is %sn, s); 5、程序填空题给定程序的功能是将在字符串 s 中出现、而未在字符串 t 中出现的字符形成一个新的字符串放在 u 中,u 中字符按原字符串中字符顺序排列,不去掉重复字符。 例如:当 s=112345,t=2467时,u 中的字符串为1135。 #include #include void fun (char *s,char *t, char *u) int i, j, sl, tl; sl = strlen(s); tl = strlen(t); for (i=0; i sl; i+) for (j=0;

21、j=tl) /找完t中所有字符都没有相同的,则存入u所指数组中/*found*/ *u+ = _2_; / si /*found*/ _3_ = 0; /*u 最后让结果数组正常结束,常规做法 main() char s100, t100, u100; printf(nPlease enter string s:); scanf(%s, s); printf(nPlease enter string t:); scanf(%s, t); fun(s, t, u); printf(the result is: %sn, u); 6、程序改错题给定程序 modi.c 中函数 fun 的功能是:求出

22、在字符串中最后一次出现的子字符串的地址,通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为 NULL。 例如,当字符串中的内容为:abcdabfabcdx,t 中的内容为:ab时,输出结果应是:abcdx。 当字符串中的内容为:abcdabfabcdx,t 中的内容为:abd时,则程序输出未找到信息:Not found!。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main 函数。#include #include #include char *fun(char *s,char *t) char *p,*r,*a; /*found*/ a=Null; /

23、 a=NULL ; NULL为代表0的符号常量,必须大写 while (*s) p=s;r=t; while (*r) /*found*/ if(r=p) r+;p+; / if(*r=*p) 匹配元素值,而不是地址 else break; if (*r=0) a=s; s+; return a; main() char s100,t100,*p; printf(nPlease enter string S:);scanf(%s,s); printf(nPlease enter substring t:);scanf(%s,t); p=fun(s,t); if(p) printf(nThe r

24、esult is: %sn,p); else printf(nNot found !n); 7、程序改错题给定程序MODI1.C中函数 fun 的功能是:将s所指字符串中最后一次出现的与t1所指字符串相同的子串替换成t2所指字符串,所形成的新串放在w所指的数组中。在此处,要求t1和t2所指字符串的长度相同。 例如,当s所指字符串中的内容为:abcdabfabc,t1所指子串中的内容为: ab,t2所指子串中的内容为:99时, 结果,在w所指的数组中的内容应为: abcdabf99c。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!给定源程序: #include #include int fun (char *s, char *t1, char *t2 , char *w) int i; char *p , *r, *a; strcpy( w, s ); /*found*/ while ( w ) / while ( *w ) 一般是用元素值或

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2