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

  • 格式:docx
  • 大小:107.54 KB
  • 文档页数:24

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

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

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

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

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

输入: 两个已经排好顺序(升序)的字符串 输出: 一个合并在一起的有序(升序)的字符串

要求: 设计一个效率尽量高的算法,对每个字符串只扫描一遍就可以了。 如果采用先进行串连接,然后再进行排序的算法,则效率太低了。

#include #include 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;ifor(j=0;jif(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; 1

a[j+1]=t; } puts(a); return 0; }

10.3 删除重复字符 背景: 输入一个长度不超过 100 的字符串,删除串中的重复字符。

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

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

#include #include int main() { char a[100],b[100]; int k,i,j; gets(a); k=strlen(a);

for(i=0;ifor(j=i+1;jif(a[j]==a[i]) a[j]='\0';

for(i=0;i1

if(a[i]!='\0') printf("%c",a[i]); printf("\n"); return 0; } 10.4 删除字符串中指定字符 输入两个字符串 s1 和 s2 ,在 s1 中删除任何 s2 中有的字符。

输入: 两个字符串 s1 和 s2 输出: 删除后的字符串 s1

#include #include

int main() { char s1[20],s2[20]; int k1,k2,i,j; gets(s1); gets(s2); k1=strlen(s1); k2=strlen(s2);

for(j=0;jfor(i=0;iif(s1[i]==s2[j]) s1[i]='\0'; 1

j=0; for(i=0;iif(s1[i]!='\0') { s1[j]=s1[i]; j++; } s1[j]='\0'; puts(s1); return 0; } 10.5 单词有多少 用空格或换行分开的字符串称为单词。输入多行字符串,直到遇到了单词 "stop" 时才停止。最后输出单词的数量。用于分割单词的空格或换行可能多于1个。

输入: 多个字符串 输出: 单词的数量

#include #include

int main() { char s[20]; int i,n=0; for(i=0;;i++) 1

{ 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 #include 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); 1

for(i=0;i{ n++; if(s1[i]==k) break; }

for(i=0;is3[i]=s1[i]; for(i=n;is3[i]=s2[i-n]; for(i=n+b;is3[i]=s1[i-b]; s3[i]='\0'; puts(s3); return 0; } 10.7 Your Ride Is Here It 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.)