中国石油大学(华东)C语言在线测评答案 第10章字符串(2013级亲测正确).docx_第1页
中国石油大学(华东)C语言在线测评答案 第10章字符串(2013级亲测正确).docx_第2页
中国石油大学(华东)C语言在线测评答案 第10章字符串(2013级亲测正确).docx_第3页
中国石油大学(华东)C语言在线测评答案 第10章字符串(2013级亲测正确).docx_第4页
中国石油大学(华东)C语言在线测评答案 第10章字符串(2013级亲测正确).docx_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

10.1 字符转换描述 提取一个字符串中的所有数字字符(0.9)将其转换为一个整数输出。输入 一个以回车符为结束标志的字符串(少于80个字符)。输出 把字符串中的所有数字字符(0.9)转换为一个整数并输出。#include#includeint main() char s80; int i,k,n=0;gets(s);k=strlen(s); for(i=0;i=0&si=9) n=n*10+(si-0); printf(%dn,n);return 0;10.2 合并字符串输入两个已经按从小到大顺序排列好的字符串,编写一个合并两个字符串的函数,使合并后的字符串,仍然是从小到 大排列。输入: 两个已经排好顺序(升序)的字符串输出: 一个合并在一起的有序(升序)的字符串要求: 设计一个效率尽量高的算法,对每个字符串只扫描一遍就可以了。如果采用先进行串连接,然后再进行排序的算法,则效率太低了。#include#includeint main() char a100,b100,t; int k,i,j; gets(a); gets(b); strcat(a,b); k=strlen(a); /*冒泡法排序*/ for(i=1;ik;i+) /*不能用字符串数组最后一项0和前面项比较,故i从1开始*/ for(j=0;jaj+1) t=aj; aj=aj+1; aj+1=t; puts(a); return 0;10.3 删除重复字符背景: 输入一个长度不超过 100 的字符串,删除串中的重复字符。输入: 输入要检查的字符串,长度不超过100个字符。例如:abacaeedabcdcd。输出: 删除重复字符后的字符串。例如:abced。#include#includeint main()char a100,b100;int k,i,j;gets(a);k=strlen(a);for(i=0;ik;i+)for(j=i+1;jk;j+)if(aj=ai) aj=0;for(i=0;ik;i+)if(ai!=0) printf(%c,ai);printf(n);return 0;10.4 删除字符串中指定字符输入两个字符串 s1 和 s2 ,在 s1 中删除任何 s2 中有的字符。输入: 两个字符串 s1 和 s2 输出: 删除后的字符串 s1#include#includeint main()char s120,s220;int k1,k2,i,j;gets(s1); gets(s2);k1=strlen(s1); k2=strlen(s2);for(j=0;jk2;j+)for(i=0;ik1;i+)if(s1i=s2j) s1i=0; j=0;for(i=0;ik1;i+)if(s1i!=0) s1j=s1i; j+; s1j=0;puts(s1);return 0;10.5 单词有多少用空格或换行分开的字符串称为单词。输入多行字符串,直到遇到了单词 stop 时才停止。最后输出单词的数量。用于分割单词的空格或换行可能多于1个。输入: 多个字符串 输出: 单词的数量#include#includeint main()char s20;int i,n=0;for(i=0;i+) scanf(%s,s); /*scanf遇空格或换行则存入下一个s20*/ n+; /*不能gets(s),它对换行空格没反应,都存入同一s,无法strcmp*/ if(strcmp(s,stop)=0) break; printf(%dn,n-1);return 0;10.6 在指定位置插入字符串输入两个字符串 s1 、 s2 和 s1 中任意字符 k ,在 s1 中的指定字符 k 第一次出现的位置处插入字符串 s2 并输出。输入: 两个字符串 s1 、 s2 和 s1 中任意字符 k输出: 插入后的字符串 s1#include#includeint main() char s150,s250,s350,k; int i,j,a,b,n=-1; gets(s1); gets(s2); a=strlen(s1); b=strlen(s2); scanf(%c,&k); for(i=0;ia;i+) n+; if(s1i=k) break; for(i=0;in;i+) s3i=s1i; for(i=n;in+b;i+) s3i=s2i-n; for(i=n+b;ia+b;i+) s3i=s1i-b; s3i=0; puts(s3);return 0;10.7 Your Ride Is HereIt is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do, however, let the groups know ahead of time which will be picked up for each comet by a clever scheme: they pick a name for the comet which, along with the name of the group, can be used to determine if it is a particular groups turn to go (who do you think names the comets?). The details of the matching scheme are given below; your job is to write a program which takes the names of a group and a comet and then determines whether the group should go with the UFO behind that comet.Both the name of the group and the name of the comet are converted into a number in the following manner: the final number is just the product of all the letters in the name, where A is 1 and Z is 26. For instance, the group USACO would be 21 * 19 * 1 * 3 * 15 = 17955. If the groups number mod 47 is the same as the comets number mod 47, then you need to tell the group to get ready! (Remember that a mod b is the remainder left over after dividing a by b; 34 mod 10 is 4.)Write a program which reads in the name of the comet and the name of the group and figures out whether according to the above scheme the names are a match, printing GO if they match and STAY if not. The names of the groups and the comets will be a string of capital letters with no spaces or punctuation, up to 6 characters long.INPUT FORMATLine 1:An upper case character string of length 1.6 that is the name of the comet.Line 2:An upper case character string of length 1.6 that is the name of the group.COMETQHVNGATOUTPUT FORMATA single line containing either the word GO or the word STAY.#include#includevoid main() char a7,b7; int i,pa=1,pb=1; gets(a); gets(b); i=0; while(ai!=0) pa=pa*(ai-A+1)%47; i+; i=0; while(bi!=0) pb=pb*(bi-A+1)%47; i+; if(pa=pb)printf(GOn); else printf(STAYn);10.8 大数相加问题描述: 编写C程序,它能以字符串形式读入两个无符号正整数m和n,计算并输出这两个整数之和输入格式: 输入由两行组成,第一行为无符号整数m,第二行为无符号整数n,且m和n的值最长25位输出格式: 输出为一行,即两个无符号整数m和n之和#include#includeint main() char a5001,b5001; int s15001,s25001,k,n=0; int ans5001; int c,alen,blen,i,maxlen,minlen; scanf(%s%s,&a,&b); alen=strlen(a); blen=strlen(b); maxlen = alen blen ? alen : blen; memset(s1,0,sizeof(s1); memset(s2,0,sizeof(s2); for( i=alen-1;i=0;i-) s1alen-i=ai-0; for( i=blen-1;i=0;i-) s2blen-i=bi-0; memset(ans,0,sizeof(ans); for(i=1;i9) if(i=maxlen) maxlen+; ansi+1+; ansi-=10; for( i=maxlen;i=1;i-) printf(%d,ansi); printf(n); if (k!=c) printf(n);return 0;10.9 字符串重排列判断一个字符串是否可以由另一个字符串通过重排字符而得到。注意,此处区分字符大小写!输入 输入只有一行,为两个字符串,字符串之间以一个空格分隔。输出如果两个字符串由同一组字符组成(且每一个字符出现次数相同),则输出“YES”;否则输出“NO”。注意YES和NO都是大写字母!#include void main()char a1000,b1000;int i,j,k,m=0;scanf (%s%s,a,b);for(i=0;ai!=0;i+) k=0;for(j=0;bj!=0;j+)if(ai=bj)bj=?;k+;m+;break; if (k=0) break;if (k=0) printf(NOn);else printf(YESn);10.10上课啦!要点名啊!小凡的老师每次上课前都要点名,但是这样就浪费了老师的上课时间。所以老师让小凡来完成点名,让小凡在早自习的时候就点好名。老师给了小凡名单,小凡只要照着名单点名就好了是不是很简单啊。输入输入有多组数据,直到文件结束。每组测试数据有三行,第一行为两个整数m, n(50 = m = n)。第二行有m个名字,名字之间用空格隔开,是小凡班上同学的名单。后面有n个名字是来上课的同学。名字间用空格隔开。名字的长度不超过20个字符。输出按照第一行的名单,每个人对应输出是否到了。到的人输出Yes,没到的人输出No。#include #include int main() int i,j,m,n,p; char a550,b550; scanf(%d%d,&m,&n); for(i=0;im;i+) scanf(%s,ai); for(i=0;in;i+) scanf(%s,bi); for(i=0;im;i+) for(j=0;jn;j+) p=strcmp(ai,bj); if(p=0) printf(YESn); break; if(j=n) printf(NOn); 10.11找第一个只出现一次的字符问题描述: 给定t个字符串,这个字符串只可能由26个小写字母组成,请你找到第一个仅出现一次的字符,如果没有符合要求的字符,就输出no。输入: 第一行是t,接下来是t个字符串,每个字符串长度小于100输出:你的输出需要由t行组成。对于每个字符串,输出第一个仅出现一次的字符,没有输出NO。#include#includeint main() int i,j,n,t,m,b200=0; char a10001000;scanf(%d,&t);for(i=0;it;i+)scanf(%s,ai);for(i=0;it;i+) m=strlen(ai);for(j=0;aij!=0;j+)for(n=0;ain!=0;n+) if(aij=ain)bj+; for(j=0;aij!=0;j+)if(bj=1)printf(%cn,aij);break;if(j=m)printf(NOn);for(j=0;aij!=0;j+)bj=0;10.12 提取数据输入一个字符串,长度不超过30,内有数字字符和非数字字符,统计其中包含了多少个非负整数,并输出这样的非负整数。输入一个字符串,最大长度为30输出输出字符串中包含的数据,一个数据一行. (不用输出总数)#includeint main()int i,sum=0;char a30;gets(a);for(i=0;ai!=0;i+)if(ai=0&ai=0&ai=9)&(ai+19)printf(%dn,sum);sum=0;10.13 判断字符串是否为回文编程,输入一个字符串,输出该字符串是否回文。输入输入为一行字符串(字符串中没有空白字符,字符串长度不超过100)。输出如果字符串是回文,输出yes;否则,输出no。#include#includeint main()int i,j,t,p=0;char a100; gets(a); t=strlen(a);j=t-1;for(i=0;i=(t/2-1);i+)if(ai!=aj) p=1;break;else j-;if(p=1)printf(non);else printf(yesn);10.14 首字母大写对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格( )、制表符(t)、回车符(r)、换行符(n)。输入输入一行:待处理的字符串(长度小于80)。输出输出一行:转换后的字符串。#includeint main() int i; char a100; gets(a); if(a0=97&a0=97&ai+1=122)ai+1=ai+1-32;puts(a);10.15 绕口令规则是:主持人给出一串字符串,要求把这串字母简化。该串字符串全部为小写英文字母。比如:aaabbbaa,则简化为3a3b2a;zzzzeeeeea,则简化为4z5e1a。依次类推。Input第一行为一个整数n,表示共有n组测试数据(1=n=100)。每组测试数据有一行,该行第一个数为字符串长度t( t = 1,000,000),然后为一行长度为t的字符串。Output对于每组输入数据输出一行,即简化后的字符串。#include#includeint main()int i,n,t,l,j,k=0,count;char a100100;scanf(%d,&n);for(i=0;in;i+)scanf(%d,&t);scanf(%s,ai);for(i=0;in;i+)l=strlen(ai);for(j=0;jl;j+=count)count=1;for(k=j+1;ak!=0;k+)if(aij=aik) count+;if(aik!=aij) break;printf(%d%c,count,aij);printf(n);10.16删除指定字符编写函数fun,其功能是:从字符串中删除指定的字符。同一字母的大、小写按照不同的字符处理。只需要提交fun函数/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */#include /* PRESET CODE END - NEVER TOUCH CODE ABOVE */void fun(char str100,char ch) int i,count=0; for(i=0;stri!=0;i+) if(stri=c

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论