当前位置:网站首页>Exercise 11-2 find week (15 points)

Exercise 11-2 find week (15 points)

2022-06-11 22:25:00 Xiaoyan y

This problem requires the implementation of functions , You can find the week according to the table below , Return the corresponding serial number .

Serial number week
0Sunday
1Monday
2Tuesday
3Wednesday
4Thursday
5Friday
6Saturday

Function interface definition :

int getindex( char *s );

function getindex Should return string s Serial number . If the parameter passed in s It's not a string representing the week , Then return to -1.

Sample referee test procedure :

#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;
}

/* Your code will be embedded here */

sample input 1:

Tuesday

sample output 1:

2

sample input 2:

today

sample output 2:

wrong input!

int getindex( char *s ){
    char *ch[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    int i;
    for(i=0;i<7;i++){
        if(strcmp(s,ch[i])==0){
            return i;
        }
    }
    return -1;
}

原网站

版权声明
本文为[Xiaoyan y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112218444748.html