设每个星期都有一个对应的序号, 其序号和星期的对照表如下:
序号 星期
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
请用程序实现:
判断指定的字符串是否是星期, 如果是, 则返回其对应的序号; 如果不是, 则返回-1.
C语言程序:
#include <stdio.h>
#include <string.h> //调用"字符串比较函数"的源文件
int getindex (char *str); //函数声明
int main ()
{
int n;
char str[7];
scanf("%s", str);
n = getindex(str);
printf("%d\n", n);
return 0;
}
int getindex (char *str)
{
int i;
char * day[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
for(i=0; i<=6; i++)
{
if(strcmp(str, day[i]) == 0) //strcmp函数——字符串比较函数
break;
}
if(i == 7)
i = -1;
return i;
}