
查找星期
本题要求实现函数,可以根据下表查找到星期,返回对应的序号。
| 序号 | 星期 | 
| 0 | Sunday | 
| 1 | Monday | 
| 2 | Tuesday | 
| 3 | Wednesday | 
| 4 | Thursday | 
| 5 | Friday | 
| 6 | Saturday | 
int getindex( char *s );函数getindex应返回字符串s序号。如果传入的参数s不是一个代表星期的字符串,则返回-1。
#include <stdio.h>
#include <string.h>
#define MAXS 80
int getindex( char *s );
int main()
{
    int n;
    char s[MAXS];
    
    scanf("%s", s);
    n = getindex(s);
    if ( n==-1 ) printf("wrong input!\n");
    else printf("%d\n", n);
    return 0;
}
/* 你的代码将被嵌在这里 */Tuesday2todaywrong input!int getindex( char *s )
{
    int i;
    char *p[7]={"Sunday","Monday","Tuesday",
    "Wednesday","Thursday","Friday","Saturday"};     
    for( i = 0 ; i < 7 ; i++ )
    {
        if(strcmp(s,p[i]) == 0)
        {
            return i;
        }
    }
    return -1;
}查找子串
本题要求实现一个字符串查找的简单函数。
char *search( char *s, char *t );函数search在字符串s中查找子串t,返回子串t在s中的首地址。若未找到,则返回NULL。
#include <stdio.h>
#define MAXS 30
char *search(char *s, char *t);
void ReadString( char s[] ); /* 裁判提供,细节不表 */
int main()
{
    char s[MAXS], t[MAXS], *pos;
    
    ReadString(s);
    ReadString(t);
    pos = search(s, t);
    if ( pos != NULL )
        printf("%d\n", pos - s);
    else
        printf("-1\n");
    return 0;
}
/* 你的代码将被嵌在这里 */The C Programming Language
ram10The C Programming Language
bored-1char *search(char *s, char *t)
{
    int i,j;
    for(i=0 ; s[i]!='\0' ;i++)
    {
        j=0;
        while(s[i+j] == t[j])
        {
            j++;
            if(t[j] == '\0')
            return s+i;
        }
    }
    return 0;
}









