Contest 11
输出月份英文名
Description
输入月份,输出对应的英文名称。要求用指针数组表示 12个月的英文名称。例如,输入5,输出May。试编写相应程序。
Input
输入一个正整数n
Output
输出对应月份的英文表示,开头字母大写,如果输出月份过大或者过小则输出wrong input!
Sample Input 1
5
Sample Output 1
May
Sample Input 2
-1
Sample Output 2
wrong input!
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
char *month[13]={"0","January","February","March","April","May","June",
"July","August","September","October","November","December"};
if (n>=1&&n<=12)
{
printf("%s",month[n]);
}
else if (n<1||n>12)
{
printf("wrong input!");
}
return 0;
}
找星期
Description
定义一个指针数组,将下表的星期信息组织起来,信息表中首字母大写,比如Monday,输入一个字符串,
在表中查找,若存在,输出该字符串在表中的序号,否则输出 “wrong input!”,本题区分大小写。试编写相应程序
Input
输入一个英文字符串
Output
输出查找结果,若存在则输出下标,否则输出wrong input!
Sample Input 1
Tuesday
Sample Output 1
2
Sample Input 2
today
Sample Output 2
wrong input!
Hint
信息表英文对应的下标:
0:Sunday
1:Monday
2:Tuesday
3:Wednesday
4:Thursday
5:Friday
6:Saturday
#include <stdio.h>
#include<string.h>
int main()
{
int n;
scanf("%d",&n);
char *day[13]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
char str[80];
scanf("%s",str);
int flag=0;
for (int i = 0; i < 7; i++)
{
if(strcmp(str,day[i])==0)
{
flag=1;
printf("%d",i);
break;
}
}
if (flag==0)
{
printf("wrong input!");
}
return 0;
}
计算最长的字符串长度
Description
输入n(n<lO)个字符串,输出其中最长字符串的有效长度(<100)。要求自定义函数 int max_len( char *s[] , int n ),
用于计算有m个元素的指针数组s中最长的字符串的长度。试编写相应程序 。
Input
输入正整数n(n<10),接下来输入n个字符串
Output
输出函数计算出的最长字符串的长度值
Sample Input 1
4
blue
yellow
red
green
Sample Output 1
6
#include<stdio.h>
#include<string.h>
int max_len( char *s[] , int n );
int main(){
int n;
scanf("%d",&n);
char *s[n];
getchar();
char a[10][100];
for(int i=0;i<n;i++){
scanf("%s",a[i]);
s[i]=a[i];
getchar();
}
printf("%d",max_len(s,n));
return 0;
}
int max_len( char *s[] , int n ){
int maxx=strlen(s[0]);
for (int i = 1; i < n; i++){
if (maxx<strlen(s[i]))maxx=strlen(s[i]);
}
return maxx;
}
字符串的拼接
Description
输入两个字符串,输出连接后的字符串。要求自定义函数 char *strcat (char *str1,char *str2),
将字符串str2复制到字符串str1的末端,并且返回字符串s的首地址。试编写相应程序。
Input
输入两行字符串,str1和str2。
Output
p=strcat (char *str1,char *str2),第一行输出p,第二行输出str1。
Sample Input 1
abc
def
Sample Output 1
abcdef
abcdef
#include<stdio.h>
#include<stdlib.h>
char *strcat (char *str1,char *str2);
void input(char* s){
int lenn=0;
char c;
while(((c=getchar())!=EOF)&&(c!='\n')){*(s+(lenn++))=c;}
//getchar();
}
int main()
{
char *str1,*str2,*p;
str1=(char *) malloc (sizeof (char) *1000);
str2=(char *) malloc (sizeof (char) *1000);
input(str1);input(str2);
p=strcat(str1,str2);
printf("%s\n%s",p,str1);
return 0;
}
char *strcat (char *str1,char *str2)
{
int lena=0,lenb=0;
while (str1[lena]!='\0')lena++;
while (str2[lenb]!='\0')lenb++;
for (int k = lena,l=0; k < lena+lenb; k++,l++)
str1[k]=str2[l];
return str1;
}
指定位置输出字符串
Description
本题要求实现一个函数,对给定的一个字符串和两个字符,打印出给定字符串中从与第一个字符匹配的位置开始到与第二个字符匹配的位置之间的所有字符。
函数接口定义:
char *match( char *s, char ch1, char ch2 );
函数match应打印s中从ch1到ch2之间的所有字符,并且返回ch1的地址。
Input
第一行一个字符串s。(|s| <= 10)
第二行两个字符ch1和ch2。
Output
第一行输出s中从ch1到ch2之间的所有字符。
第二行输出s中从ch1开始到末尾的所有字符。
Sample Input 1
program
r g
Sample Output 1
rog
rogram
Sample Input 2
program
z o
Sample Output 2
Sample Input 3
program
g z
Sample Output 3
gram
gram
#include <stdio.h>
#include <stdlib.h>
char* match(char* s,char ch1,char ch2 ){
char *p;
for(int i=0;s[i]!='\0';i++){
if(*(s+i)==ch1){
p=s+i;
for(int j=i;s[j]!='\0';j++){
printf("%c",*(s+j));
if(*(s+j)==ch2)break;
}
printf("\n");
return p;
}
}
p=s-1;
return p;
}
int main(){
char *s,ch1,ch2;
s=(char *)malloc(sizeof(char)*1000);
scanf("%s",s);
getchar();
scanf("%c",&ch1);
getchar();
scanf("%c",&ch2);
char *p=NULL;
p=match(s,ch1,ch2);
printf("%s",p);
return 0;
}
查找子串
Description
本题要求实现一个字符串查找的简单函数。
函数接口定义:
char *search( char *s, char *t );
函数search在字符串s中查找子串t,返回子串t在s中的首地址。若未找到,则返回NULL。
Input
第一行一个字符串s。(|s|<30)
第二行一个字符串t。(|t|<30)
Output
一个整数,表示t在s中的首地址。
Sample Input 1
The C Programming Language
ram
Sample Output 1
10
Sample Input 2
The C Programming Language
bored
Sample Output 2
-1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* search(char* s,char *t){
int lena=strlen(s),lenb=strlen(t);
int i,j;
for(i=0,j=0;i<lena&&j<lenb;){
if(*(s+i)==*(t+j)){i++;j++;}
else{
j=0;
i=i+1;
}
}
if(j>=lenb)return s+i-j-1;
else return NULL;
};
void input(char* s){
int lenn=0;
char c;
while(((c=getchar())!=EOF)&&(c!='\n')){*(s+(lenn++))=c;}
getchar();
}
int main(){
char *s,*t,*p;
s=(char *)malloc(sizeof(char)*66);
t=(char *)malloc(sizeof(char)*66);
input(s);input(t);
p=search(s,t);
if(p==NULL)printf("-1");
else printf("%ld",p-s);
return 0;
}