Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符数组能否在c中使用开关大小写_C - Fatal编程技术网

字符数组能否在c中使用开关大小写

字符数组能否在c中使用开关大小写,c,C,这是我的密码。我在开关箱部分出错。开关箱未检查这些箱子。请帮帮我。 提前感谢。使用c,我知道的唯一方法是: void main() { char day[20]; printf("Enter the short name of day"); scanf("%s", day); switch(day) { case "sun": printf("sunday"); break; case "mon": printf("mo

这是我的密码。我在开关箱部分出错。开关箱未检查这些箱子。请帮帮我。
提前感谢。

使用c,我知道的唯一方法是:

void main()
{
  char day[20];
  printf("Enter the short name of day");

  scanf("%s", day);

  switch(day)
  {
    case "sun":
      printf("sunday");
      break;
    case "mon":
      printf("monday");
      break;
    case "Tue":
      printf("Tuesday");
      break;
    case "wed":
      printf("wednesday");
      break;
    case "Thu":
      printf("Thursday");
      break;
    case "Fri":
      printf("friday");
      break;
    case "sat":
      printf("saturday");
      break;
  }
}
这应该行得通。
(但仅限于4字节或更少的字符串)

这将字符串视为4字节整数。

这被认为是一种丑陋的“粗俗”风格,一点也不好。
但它确实能满足你的需求

if (strcmp(day, "sun") == 0) 
{
   printf("sunday");
} 
else if (strcmp(day, "mon") == 0)
{
   printf("monday");
}
/* more else if clauses */
else /* default: */
{
}

在MSVC2010中测试

在常量列表中查找字符串。如果找到,请使用索引进行切换


一个
enum daysOfWeek{EwdMon,EwdTues,EwdWed….}
是一个很好的起点。

如前所述,
switch
语句在C语言中无法处理字符串。您可以这样做,使代码更加简洁:

#include "Winsock2.h"
#pragma comment(lib,"ws2_32.lib")

void main()
{
  char day[20];
  printf("Enter the short name of day");

  scanf("%s", day);

  switch(htonl(*((unsigned long*)day)))
  {
    case 'sun\0':
      printf("sunday");
      break;
    case 'mon\0':
      printf("monday");
      break;
    case 'Tue\0':
      printf("Tuesday");
      break;
    case 'wed\0':
      printf("wednesday");
      break;
    case 'Thu\0':
      printf("Thursday");
      break;
    case 'Fri\0':
      printf("friday");
      break;
    case 'sat\0':
      printf("saturday");
      break;
  }
}
#包括
静态结构日{
const char*abbrev;
常量字符*名称;
}天[]={
{“太阳”,“星期日”},
{“周一”、“周一”},
{“星期二”,“星期二”},
{“星期三”,“星期三”},
{“星期四”,“星期四”},
{“星期五”,“星期五”},
{“星期六”,“星期六”},
};
int main()
{
int i;
查日[20];
printf(“输入日期的简称”);
scanf(“%s”,天);
对于(i=0;i
在C中,除了整数之外,你不能
切换
。请阅读。如果不是只有七天,我建议使用哈希。请使用宏和模板
#include <stdio.h>

static struct day {
  const char *abbrev;
  const char *name;
} days[] = {
  { "sun", "sunday"    },
  { "mon", "monday"    },
  { "tue", "tuesday"   },
  { "wed", "wednesday" },
  { "thu", "thursday"  },
  { "fri", "friday"    },
  { "sat", "saturday"  },
};

int main()
{
  int i;
  char day[20];
  printf("Enter the short name of day");

  scanf("%s", day);

  for (i = 0; i < sizeof(days) / sizeof(days[0]); i++) {
    if (strcasecmp(day, days[i].abbrev) == 0) {
      printf("%s\n", days[i].name);
      break;
    }
  }

  return 0;
}