




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第11章 字符串和字符串函数编程练习 1设计并测试一个函数,可以从输入读取n个字符(包括空格、制表符和换行符),把结果存储在一个数组中,这个数组的地址通过参数来传递。 #include void input(char *p, int n);int main(void) char a81; int n; puts(input the char number of your string:); scanf(%d,&n); getchar(); /滤去回车 puts(input your string:); input(a,n); puts(a); return 0;void input(char *p, int n) int i; for (i=0;in;i+) *(p+i) = getchar(); *(p+i) = 0; 2修改并测试练习1中的函数,使得可以在n个字符后,或第一个空格、制表符、换行符后停止读取输入,由上述情况中最先被满足的那个终止读取(不能用scanf()函数)。 #include #include void input(char *p, int n);int main(void) char a81; int n; puts(input the char number of your string:); scanf(%d,&n); getchar(); /滤去回车 puts(input your string:); input(a,n); puts(a); return 0;void input(char *p, int n) int i; for (i=0;in;i+) *(p+i) = getchar(); if( isspace(*(p+i) ) break; *(p+i) = 0; 3设计并测试一个函数,其功能是读取输入行里的第一个单词到数组,并丢掉该行中其他的字符。一个单词的定义是一串字符,其中不含空格、制表符和换行符。 #include #include void word(char *p);int main(void) char a81; puts(input your string:); gets(a); word(a); puts(a); return 0;void word(char *p) int begin,end; for(begin=0; isspace( *(p+begin) ) ;begin+) continue; for(end=begin; !isspace( *(p+end) ) ;end+) continue; *(p+end) = 0; for(; *(p + begin) != 0; p+) *p = *(p + begin); *p = 0; 4设计并测试一个函数,其功能是搜索由函数的第一个参数指定的字符串,在其中查找由函数的第二个参数指定的字符的第一次出现的位置。如果找到,返回指向这个字符的指针:如果没有找到,返回空字符(这种方式和strchr()函数的功能一样)。在一个使用循环语句为这个函数提供输入的完整程序中进行测试。 #include char *mystrchr(char *, char );int main(void) char str81; char ch; char *p; do puts(input range string:); gets(str); puts(input match char:); ch = getchar(); getchar(); p = mystrchr(str, ch); if ( p ) puts(Find!); puts(p); else puts(Cant find!); puts(input any char except q to go on.); gets(str); while(*str != q); puts(Quit.); return 0;char *mystrchr(char *p, char ch) char *p_save = p; if(*p = 0) return NULL; while(1) if(*p = ch) return p_save; else if(*+p = 0) return NULL; p_save = p; 5编写一个函数is_witlun()。它接受两个参数,一个是字符,另一个是字符串指针。其功能是如果字符在字符串中,就返回一个非O值(真);如果字符不在字符串中,就返回O值(假)。在一个使用循环语句为这个函数提供输入的完整程序中进行测试。 #include int is_within(char *p, char ch);int main(void) char str81; char ch; do puts(input range string:); gets(str); puts(input match char:); ch = getchar(); getchar(); if ( is_within(str, ch) ) puts(Find!); else puts(Cant find!); puts(input any char except q to go on.); ch = getchar(); getchar(); while(ch != q); puts(Quit.); return 0;int is_within(char *p, char ch) while(*p != 0) if(*p = ch) return 1; p+; return 0; 6strncpy (sl,s2,n)函数从s2复制n个字符给sl,并在必要时截断s2或为其填充额外的空字符。如果s2的长度等于或大于n,目标字符串就没有标志结束的空字符。函数返回sl。自己编写这个函数,并在一个使用循环语句为这个函数提供输入的完整程序中进行测试。 #include char *mystrncpy(char *p1, char *p2, int n);int main(void) char str181; char str281; int n; do puts(input string1:); gets(str1); puts(input string2:); gets(str2); puts(input the number of copying char:); scanf(%d,&n); getchar(); puts(After copying:); puts(mystrncpy(str1, str2, n); puts(input any char except q to go on.); gets(str1); while(*str1 != q); puts(Quit.); return 0;char *mystrncpy(char *p1, char *p2, int n) char *p=p1; while(*p1+ != 0) continue; *-p1 = *p2; n-; while(n0 & *p2 != 0) *+p1 = *+p2; n-; return p; 7编写一个函数string_in(),它接受两个字符串指针参数。如果第二个字符串被包含在第一个字符串中,函数就返回被包含的字符开始的地址。例如,string_in(hats,at)返回hats中a的地址,则,函数返回空指针。在一个使用循环语句为这个函数提供输入的完整程序中进行测试。 #include char *string_in(char *p1, char *p2);int main(void) char str181; char str221; char *p; do puts(input range string:); gets(str1); puts(input match string:); gets(str2); p = string_in(str1, str2); if ( p ) puts(Find!); puts(p); else puts(Cant find!); puts(input any char except q to go on.); gets(str1); while(*str1 != q); puts(Quit.); return 0;char *string_in(char *p1, char *p2) char *p1_save = p1, *p2_save = p2; if(*p1 = 0 | *p2 = 0) return NULL; while(1) if(*p1 = *p2) if(*+p2 = 0) return p1_save; if(*+p1 = 0) return NULL; else if(*+p1 = 0) return NULL; p1_save = p1; p2 = p2_save; 8编写一个函数,其功能是使输入字符串反序。在一个使用循环语句为这个函数提供输入的完整程序中进行测试。 #include void reverse(char *p);int main(void) char str81; do puts(input a string:); gets(str); reverse(str); puts(str); puts(input any char except q to go on.); gets(str); while(*str != q); puts(Quit.); return 0;void reverse(char *p) int i,n; char temp; for(n=0; *(p+n) != 0; n+) continue; n-; for(i=0; i n-i; i+) temp = pi; pi = pn-i; pn-i = temp; 9编写一个函数。其参数为一个字符串,函数删除字符串中的空格。在一个可以循环读取的程序中进行测试,直到用户输入空行。对于任何输入字符串,函数都应该适用并可以显示结果。 #include void delspace(char *p1);int main(void) char str81; do puts(input a string:); gets(str); delspace(str); puts(str); puts(input any char except q to go on.); gets(str); while(*str != q); puts(Quit.); return 0;void delspace(char *p1) char *p2; while (*p1 != 0 ) if (*p1 = ) p2 = p1; while(*p2 != 0) *p2 = *(p2+1); p2+; p1-; /抵消下面的p1+ p1+; 10.编写一个程序,读取输入,直到读入了10个字符串或遇到EOF,由二者中最先被满足的那个终止读取过程。这个程序可以为用户提供一个有5个选项的菜单:输出初始字符串列表、按ASCII顺序输出字符串、按长度递增顺序输出字符串、按字符串中第一个单词的长度输出字符串和退出。菜单可以循环,直到用户输入退出请求。当然,程序要能真正完成菜单中的各项功能。 #include #include #include void origin_put(char *p, int n);void ascii_put(char *p, int n);void length_put(char *p, int n);int first_word_length(char *p);int first_word_length(char *p);void word_put(char *p, int n);int main(void) char str1081; char *p10; char command10; int n; while(1) n = 0; puts(input no more than 10 strings finished by EOF (Z):); do if ( gets(strn) = NULL ) break; pn = strn; n+; while( n10 ); puts(select:); puts(a. put originally); puts(b. put in order of ascii); puts(c. put in order of strings length); puts(d. put in order of first words length); puts(e. input strings again); puts(q. quit); do gets(command); switch(command0) case a: puts(put originally:); origin_put(p,n); break; case b: puts(put in order of ascii:); ascii_put(p,n); break; case c: puts(put in order of strings length:); length_put(p,n); break; case d: puts(put in order of first words length:); word_put(p,n); break; case e: break; default : puts(Quit.); return 0; while( command0 != e ); void origin_put(char *p, int n) int i; for(i=0; in; i+) puts(pi);void ascii_put(char *p, int n) int i,j; char *temp; for(i=0; in; i+) for(j=0; j 0) temp = pj; pj = pj+1; pj+1 = temp; origin_put(p,n);void length_put(char *p, int n) int i,j; char *temp; for(i=0; in; i+) for(j=0; j strlen(pj+1) ) temp = pj; pj = pj+1; pj+1 = temp; origin_put(p,n);int first_word_length(char *p) int i=0; for (; !isalpha(*p); p+) if (*p = 0) return 0; for (i=1; isalpha(pi); i+) continue; return i;void word_put(char *p, int n) int i,j; char *temp; for(i=0; in; i+) for(j=0; j first_word_length(pj+1) ) temp = pj; pj = pj+1; pj+1 = temp; origin_put(p,n); 11.编写一个程序。功能是读取输入,直到遇到EOF,并报告单词数、大写字母数、小写字母数、标点符号数和数字字符数。使用ctype.h系列的函数。 #include #include int main(void) int word=0,upper=0,lower=0,punct=0,digit=0,begin=0; char ch; while( ( ch = getchar() ) != EOF ) if( isalpha(ch) ) if (begin = 0) word+; begin = 1; else begin = 0; if( isupper(ch) ) upper+; if( islower(ch) ) lower+; if( ispunct(ch) ) punct+; if( isdigit(ch) ) digit+; printf(word:%dnupper:%dnlower:%dnpunct:%dndigit:%dn,word,upper,lower,punct,digit); return 0; 12.编写一个程序,按照相反的单词顺序显示命令行参数。即,如果命令行参数是see you later,程序的显示应该为later you see。 #include #include #include int main(void) char str81; char *p, temp; int i,n,length; gets(str); for (p = str,i=0,n = strlen(str); i1) for(i=0; ilength/2; i+) temp = *(p-1-i); *(p-1-i) = *(p-length+i); *(p-length+i) = temp; length = 0; while(*p+ != 0); puts(str); return 0; 13.编写一个计算乘幂的基于命令行的程序。第一个命令行参数为double类型数,作为幂的底数;第二个参数为整数,作为幂的指数。 #include double mypower(double base, int exp);int main(void) double base; int exp; printf(input base number and exp:); scanf(%lf%d, &base, &exp); printf(%g%d= %gn, base, exp, mypower(base,exp) ); return 0;double mypower(double base, int exp) double power = 1; if (exp0) while( exp- 0 ) power *= base; else if(base!=0) power = 1; else power = 1/base;/0的0次幂应是一个无意义数 return power; 14.使用字符分类函数实现atoi()函数。 #include #include #include double myatof(char *p);int main(void) char a30=0; while(a0 != q) puts(input a double without +-e:); gets(a); printf(atof: %.5lfn,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论