6-14 查找星期 (15分)


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM