c语言第八章字符串编程答案

字符转换
描述

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

输入

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

输出

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


#include<stdio.h>
int main()
{
char str[80];
int i;
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]<='9'&&str[i]>='0') printf("%c",str[i]);
}
printf("\n");
}



合并字符串
输入两个已经按从小到大顺序排列好的字符串,编写一个合并两个字符串的函数,使合并后的字符串,仍然是从小到 大排列。

输入:
两个已经排好顺序(升序)的两个字符串

输出:
一个合并在一起的有序(升序)的字符串

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



#include<stdio.h>
int main()
{
char str1[80],str2[80],t;
int i,n,j;
gets(str1);
gets(str2);
strcat(str1,str2);
n=strlen(str1);
for(j=1; j<=n; j++)
for(i=0; i<n-j; i++)
if(str1[i]>=str1[i+1])
{
t=str1[i];
str1[i]=str1[i+1];
str1[i+1]=t;
}
puts(str1);
}




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

输入:

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

输出:

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



#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int i,j;
gets(str);
for(i=0; str[i]!=0; i++)
for(j=0; str[j]!=0; j++)
if(str[j]==str[i]&&i!=j) str[j]='\t';
for(i=0; str[i]!=0; i++)
if(str[i]!='\t') printf("%c",str[i]);
printf("\n");
}




删除指定字符

输入两个字符串 s1 和 s2 ,在 s1 中删除任何 s2 中有的字符。例如, s1 :“ abc123ad ”, s2 :“ a1 ” ,则输出“bc23d ”。

输入: 两个字符串 s1 和 s2

输出: 删除后的字符串 s1



#include<stdio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
int i,j;
gets(str1);
gets(str2);
for(i=0; str1[i]!=0; i++)
for(j=0; str2[j]!=0; j++)
if(str2[j]==str1[i]) str1[i]='\t';
for(i=0; str1[i]!=0; i++)
if(str1[i]!='\t') printf("%c",str1[i]);
printf("\n");
}



【设计型】8.5 单词有多少
成绩: 100 / 折扣: 0.8
用空格或换行分开的字符串称为单词。输入多行字符串,直到遇到了单词 "stop" 时才停止。最后输出单词的数量。用于分割单词的空格
或换行可能多于1个。

输入

: 多个字符串

输出: 单词的数量



#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i,n=0;
for(i=0;; i++)
{
scanf("%s",str);
n++;
if(strcmp(str,"stop")==0) break;
}
printf("%d\n",n-1);
return 0;
}





8.6 在指定位置插入字符串
成绩: 100 / 折扣: 0.8
输入两个字符串 s1 、 s2 和 s1 中任意字符 k ,在 s1 中的指定字符 k 第一次出现的位置处插入字符串 s2 并输出。

输入: 两个字符串 s1 、 s2 和 s1 中任意字符 k

输出: 插入后的字符串 s1


#include<stdio.h>
#include<string.h>
void main()
{
char s1[50],s2[50],s3[50];
char a;
int i,j,n,m,c=-1;
gets(s1);
gets(s2);
n=strlen(s1);
m=strlen(s2);
scanf("%c",&a);
for(i=0; s1[i]!=0; i++)
{
c++;
if(s1[i]==a) break;
}
for(i=0; i<c; i++)
s3[i]=s1[i];
for(i=c; i<m+c; i++)
s3[i]=s2[i-c];
for(i=c+m; i<=m+n; i++)
s3[i]=s1[i-m];
puts(s3);
}




【探索性】8.8 大数相加(哈哈哈哈哈哈哈哈哈哈)
成绩: 100 / 折扣: 0.8
问题描述:
编写C程序,它能以字符串形式读入两个无符号正整数m和n,计算并输出这两个整数之和
输入格式:
输入由两行组成,第一行为无符号整数m,第二行为无符号整数n,且m和n的值最长25位
输出格式:
输出为一行,即两个无符号整数m和n之和
输入样例:
9999888888
355729288
输出样例:
10355618176

#include<stdio.h>
#include<string.h>
int main()
{
char str1[26],str2[26];
int s[27];
int i,min,max,t,n=0,a;
gets(str1);
gets(str2);
for(i=0; i<strlen(str1)/2+1; i++)
if(i<strlen(str1)-1-i)
{
t=str1[i];
str1[i]=str1[strlen(str1)-1-i];
str1[strlen(str1)-1-i]=t;
}
for(i=0; i<strlen(str2)/2+1; i++)
if(i<strlen(str2)-1-i)
{
t=str2[i];
str2[i]=str2[strlen(str2)-1-i];
str2[strlen(str2)-1-i]=t;
}
if(strlen(str1)>strlen(str2))
{
max=strlen(str1);
min=strlen(str2);
for(i=min; i<max; i++)
str2[i]='0';
str2[max]='\0';
s[0]=0;
for(i=0; i<max; i++)
{
a=(str1[i]-'0')+(str2[i]-'0')+s[i];
s[i]=((str1[i]-'0')+(str2[i]-'0')+s[i])%10;
s[i+1]=a/10;
n++;
}
}
else
{
max=strlen(str2);
min=strlen(str1);
for(i=min; i<=max; i++)
str1[i]='0';
str1[max]='\0';
s[0]=0;
for(i=0; i<max; i++)
{
a=(str1[i]-'0')+(str2[i]-'0')+s[i];
s[i]=((str1[i]-'0')+(str2[i]-'0')+s[i])%10;
s[i+1]=a/10;
n++;
}

}
for(i=0; i<n/2+1; i++)
if(i<n-i)
{
t=s[i];
s[i]=s[n-i];
s[n-i]=t;
}
if(s[0]!=0)
for(i=0; i<n+1; i++)
printf("%d",s[i]);
else
for(i=1; i<n+1; i++)
printf("%d",s[i]);
printf("\n");
return 0;
}


【研究创新性】8.7 Your Ride Is Here
成绩: 100 / 折扣: 0.8
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.)

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.



#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");
else
printf("STAY\n");
}











数组中数字组成的最大数(修改)

#include<stdio.h>
int main()
{
int t,m,i,j,n;
int a[100];
scanf("%d",&m);
for(i=0; i<100; i++)
{
a[i]=(m%10);
m=m/10;
n++;
if(m==0) break;
}
for(j=0; j<n; j++)
for(i=0; i<n-1-j; i++)
if(a[i]<a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
for(i=0; i<n; i++)
printf("%d",a[i]);
printf("\n");
}

相关文档
最新文档