6-14 查找星期 (15分)
本題要求實現函數,可以根據下表查找到星期,返回對應的序號。
序號 | 星期 |
---|---|
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; } /* 你的代碼將被嵌在這里 */
輸入樣例1:
Tuesday
輸出樣例1:
2
輸入樣例2:
today
輸出樣例2:
wrong input!
int getindex( char *s )
{
char m[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int i;
for(i=0;i<7;i++)
{
if(strcmp(m[i],s)==0)
return i;
}
return -1;
}