中国石油大学(华东)c语言在线测评答案 第10章字符串(级亲测正确)doc资料

  • 格式:docx
  • 大小:93.09 KB
  • 文档页数:20

10.1 字符转换描述提取一个字符串中的所有数字字符(‘0’...‘9’)将其转换为一个整数输出。

输入一个以回车符为结束标志的字符串(少于80个字符)。

输出把字符串中的所有数字字符(‘0’...‘9’)转换为一个整数并输出。

#include<stdio.h>#include<string.h>int main(){char s[80];int i,k,n=0;gets(s);k=strlen(s);for(i=0;i<k;i++)if(s[i]>='0'&&s[i]<='9')n=n*10+(s[i]-'0');printf("%d\n",n);return 0;}10.2 合并字符串输入两个已经按从小到大顺序排列好的字符串,编写一个合并两个字符串的函数,使合并后的字符串,仍然是从小到大排列。

输入:两个已经排好顺序(升序)的字符串输出:一个合并在一起的有序(升序)的字符串要求:设计一个效率尽量高的算法,对每个字符串只扫描一遍就可以了。

如果采用先进行串连接,然后再进行排序的算法,则效率太低了。

#include<stdio.h>#include<string.h>int main(){char a[100],b[100],t;int k,i,j;gets(a); gets(b);strcat(a,b);k=strlen(a);/*冒泡法排序*/for(i=1;i<k;i++) /*不能用字符串数组最后一项'\0'和前面项比较,故i从1开始*/ for(j=0;j<k-i;j++)if(a[j]>a[j+1]){ t=a[j];a[j]=a[j+1];a[j+1]=t; }puts(a);return 0;}10.3 删除重复字符背景:输入一个长度不超过 100 的字符串,删除串中的重复字符。

输入:输入要检查的字符串,长度不超过100个字符。

例如:abacaeedabcdcd。

输出:删除重复字符后的字符串。

例如:abced。

#include<stdio.h>#include<string.h>int main(){char a[100],b[100];int k,i,j;gets(a);k=strlen(a);for(i=0;i<k;i++)for(j=i+1;j<k;j++)if(a[j]==a[i]) a[j]='\0';for(i=0;i<k;i++)if(a[i]!='\0')printf("%c",a[i]);printf("\n");return 0;}10.4 删除字符串中指定字符输入两个字符串 s1 和 s2 ,在 s1 中删除任何 s2 中有的字符。

输入:两个字符串 s1 和 s2 输出:删除后的字符串 s1#include<stdio.h>#include<string.h>{char s1[20],s2[20];int k1,k2,i,j;gets(s1); gets(s2);k1=strlen(s1); k2=strlen(s2);for(j=0;j<k2;j++)for(i=0;i<k1;i++)if(s1[i]==s2[j]) s1[i]='\0';j=0;for(i=0;i<k1;i++)if(s1[i]!='\0'){ s1[j]=s1[i]; j++; }s1[j]='\0';puts(s1);return 0;}10.5 单词有多少用空格或换行分开的字符串称为单词。

输入多行字符串,直到遇到了单词 "stop" 时才停止。

最后输出单词的数量。

用于分割单词的空格或换行可能多于1个。

输入:多个字符串输出:单词的数量#include<stdio.h>#include<string.h>{char s[20];int i,n=0;for(i=0;;i++){ scanf("%s",s); /*scanf遇空格或换行则存入下一个s[20]*/n++; /*不能gets(s),它对换行空格没反应,都存入同一s[],无法strcmp*/if(strcmp(s,"stop")==0) break;}printf("%d\n",n-1);return 0;}10.6 在指定位置插入字符串输入两个字符串 s1 、 s2 和 s1 中任意字符 k ,在 s1 中的指定字符 k 第一次出现的位置处插入字符串 s2 并输出。

输入:两个字符串 s1 、 s2 和 s1 中任意字符 k输出:插入后的字符串 s1#include<stdio.h>#include<string.h>int main(){char s1[50],s2[50],s3[50],k;int i,j,a,b,n=-1;gets(s1); gets(s2);a=strlen(s1); b=strlen(s2);scanf("%c",&k);for(i=0;i<a;i++){ n++;if(s1[i]==k) break;}for(i=0;i<n;i++)s3[i]=s1[i];for(i=n;i<n+b;i++)s3[i]=s2[i-n];for(i=n+b;i<a+b;i++)s3[i]=s1[i-b];s3[i]='\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 group's 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 group's number mod 47 is the same as the comet's 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 area 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 FORMATCOMETQHVNGATOUTPUT FORMATA single line containing either the word "GO" or the word "STAY".#include<stdio.h>#include<string.h>void main(){char a[7],b[7];int i,pa=1,pb=1;gets(a);gets(b);i=0;while(a[i]!='\0'){pa=pa*(a[i]-'A'+1)%47;i++;}i=0;while(b[i]!='\0'){pb=pb*(b[i]-'A'+1)%47;i++;}if(pa==pb)printf("GO\n");elseprintf("STAY\n");}10.8 大数相加问题描述:编写C程序,它能以字符串形式读入两个无符号正整数m和n,计算并输出这两个整数之和输入格式:输入由两行组成,第一行为无符号整数m,第二行为无符号整数n,且m和n的值最长25位输出格式:输出为一行,即两个无符号整数m和n之和#include<stdio.h>#include<string.h>int main(){char a[5001],b[5001];int s1[5001],s2[5001],k,n=0;int ans[5001];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--)s1[alen-i]=a[i]-'0';for( i=blen-1;i>=0;i--)s2[blen-i]=b[i]-'0';memset(ans,0,sizeof(ans));for(i=1;i<=maxlen;i++){ans[i] += s1[i] + s2[i];if(ans[i]>9){if(i==maxlen)maxlen++;ans[i+1]++;ans[i]-=10;}}for( i=maxlen;i>=1;i--)printf("%d",ans[i]);printf("\n");if (k!=c)printf("\n");return 0;}10.9 字符串重排列判断一个字符串是否可以由另一个字符串通过重排字符而得到。