资源描述
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>
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;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>
int main()
{
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 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.)
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 FORMAT
Line 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.
COMETQ
HVNGAT
OUTPUT FORMAT
A 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");
else
printf("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 字符串重排列
判断一个字符串是否可以由另一个字符串通过重排字符而得到。注意,此处区分字符大小写!
输入 输入只有一行,为两个字符串,字符串之间以一个空格分隔。
输出
如果两个字符串由同一组字符组成(且每一个字符出现次数相同),则输出“YES”;
否则输出“NO”。注意YES和NO都是大写字母!
#include <stdio.h>
void main()
{
char a[1000],b[1000];
int i,j,k,m=0;
scanf ("%s%s",a,b);
for(i=0;a[i]!='\0';i++)
{ k=0;
for(j=0;b[j]!='\0';j++)
if(a[i]==b[j])
{
b[j]='?';
k++;
m++;
break;
}
if (k==0) break;
}
if (k==0) printf("NO\n");
else printf("YES\n");
}
10.10上课啦!要点名啊!
小凡的老师每次上课前都要点名,但是这样就浪费了老师的上课时间。所以老师让小凡来完成点名,让小凡在早自习的时候就点好名。老师给了小凡名单,小凡只要照着名单点名就好了是不是很简单啊。
输入
输入有多组数据,直到文件结束。每组测试数据有三行,第一行为两个整数m, n(50 >= m >= n)。第二行有m个名字,名字之间用空格隔开,是小凡班上同学的名单。后面有n个名字是来上课的同学。名字间用空格隔开。名字的长度不超过20个字符。
输出
按照第一行的名单,每个人对应输出是否到了。到的人输出Yes,没到的人输出No。
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,m,n,p;
char a[5][50],b[5][50];
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
scanf("%s",a[i]);
for(i=0;i<n;i++)
scanf("%s",b[i]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
p=strcmp(a[i],b[j]);
if(p==0)
{
printf("YES\n");
break;
}
}
if(j==n)
printf("NO\n");
}
}
10.11找第一个只出现一次的字符
问题描述:
给定t个字符串,这个字符串只可能由26个小写字母组成,请你找到第一个仅出现一次的字符,如果没有符合要求的字符,就输出no。
输入: 第一行是t,接下来是t个字符串,每个字符串长度小于100
输出:
你的输出需要由t行组成。
对于每个字符串,输出第一个仅出现一次的字符,没有输出NO。
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,n,t,m,b[200]={0};
char a[1000][1000];
scanf("%d",&t);
for(i=0;i<t;i++)
scanf("%s",a[i]);
for(i=0;i<t;i++)
{
m=strlen(a[i]);
for(j=0;a[i][j]!='\0';j++)
{
for(n=0;a[i][n]!='\0';n++)
{
if(a[i][j]==a[i][n])
{
b[j]++;
}
}
}
for(j=0;a[i][j]!='\0';j++)
{
if(b[j]==1)
{
printf("%c\n",a[i][j]);
break;
}
}
if(j==m)
printf("NO\n");
for(j=0;a[i][j]!='\0';j++)
b[j]=0;
}
}
10.12 提取数据
输入一个字符串,长度不超过30,内有数字字符和非数字字符,统计其中包含了多少个非负整数,并输出这样的非负整数。
输入
一个字符串,最大长度为30
输出
输出字符串中包含的数据,一个数据一行. (不用输出总数)
#include<stdio.h>
int main()
{
int i,sum=0;
char a[30];
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]>='0'&&a[i]<='9')
sum=sum*10+(a[i]-'0');
if((a[i]>='0'&&a[i]<='9')&&(a[i+1]<'0'||a[i+1]>'9'))
{
printf("%d\n",sum);
sum=0;
}
}
}
10.13 判断字符串是否为回文
编程,输入一个字符串,输出该字符串是否回文。
输入
输入为一行字符串(字符串中没有空白字符,字符串长度不超过100)。
输出
如果字符串是回文,输出yes;否则,输出no。
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,t,p=0;
char a[100];
gets(a);
t=strlen(a);
j=t-1;
for(i=0;i<=(t/2-1);i++)
{
if(a[i]!=a[j])
{
p=1;
break;
}
else j--;
}
if(p==1)
printf("no\n");
else printf("yes\n");
}
10.14 首字母大写
对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。
输入
输入一行:待处理的字符串(长度小于80)。
输出
输出一行:转换后的字符串。
#include<stdio.h>
int main()
{
int i;
char a[100];
gets(a);
if(a[0]>=97&&a[0]<=122)
a[0]=a[0]-32;
for(i=1;a[i]!='\0';i++)
{
if(a[i]==' '&&a[i+1]>=97&&a[i+1]<=122)
a[i+1]=a[i+1]-32;
}
puts(a);
}
10.15 绕口令
规则是:主持人给出一串字符串,要求把这串字母简化。该串字符串全部为小写英文字母。
比如:aaabbbaa,则简化为3a3b2a;zzzzeeeeea,则简化为4z5e1a。依次类推。
Input
第一行为一个整数n,表示共有n组测试数据(1<=n<=100)。每组测试数据有一行,该行第一个数为字符串长度t( t <= 1,000,000),然后为一行长度为t的字符串。
Output
对于每组输入数据输出一行,即简化后的字符串。
#include<stdio.h>
#include<string.h>
int main()
{
int i,n,t,l,j,k=0,count;
char a[100][100];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&t);
scanf("%s",a[i]);
}
for(i=0;i<n;i++)
{
l=strlen(a[i]);
for(j=0;j<l;j+=count)
{
count=1;
for(k=j+1;a[k]!='\0';k++)
{
if(a[i][j]==a[i][k]) count++;
if(a[i][k]!=a[i][j]) break;
}
printf("%d%c",count,a[i][j]);
}
printf("\n");
}
}
10.16删除指定字符
编写函数fun,其功能是:从字符串中删除指定的字符。同一字母的大、小写按照不同的字符处理。只需要提交fun函数
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include <stdio.h>
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
void fun(char str[100],char ch)
{
int i,count=0;
for(i=0;str[i]!='\0';i++)
if(str[i]==ch)
str[i]='0';
for(i=0;str[i]!='\0';i++)
if(str[i]!='0')
str[count++]=str[i];
str[count]='\0';
}
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
int main()
{
char str[80];
char ch;
gets(str);
scanf("%c",&ch);
fun(str,ch);
printf("%s\n",str);
return 0;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
10.17处理字符串
编写函数fun,其功能是:将数组s存放的字符串中的所有数字字符移到所有非数字字符之后,并保持数字字符串和非数字字符串原有的先后次序。
例如,s中的字符串为:def35adh3kjsdf7。执行结果为:defadhkjsdf3537。
提醒:本题提交的时候,只需要提交fun函数就可以。
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include <stdio.h>
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
void fun(char s[])
{
char a[80],b[80];
int i,j,x=0,y=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='0'&&s[i]<='9')
{
a[x]=s[i];
x++;
}
else
{
s[y]=s[i];
y++;
}
}
a[x]='\0';
s[y]='\0';
strcat(s,a);
}
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
int main()
{
char s[80];
gets(s);
fun(s);
puts(s);
return 0;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
20
展开阅读全文