【问题标题】:String Search Operation in CC中的字符串搜索操作
【发布时间】:2014-06-07 16:05:20
【问题描述】:

我想要月份索引的值,但是 k 的值是错误的。可以引起?函数有错误但找不到。

#include <stdio.h>
#include <string.h>
#define SIZE 20
int get_month(const char *str, char *array[])
{

int k;
for (k = 0; k < 12; ++k)
    if (!strcmp(array[k], str)
    return k ;
return 0;
}
int main()
{
char s[SIZE];
char *months[] = {"January"," February","March", "April","May","June","July","August ","September", " October"," November","December" };
int result;
printf("enter the month : ");
gets(s);
result = get_month(s,months);
   if (result)
       printf("%s - %d.  is the month of the year.\n", s, result);
      else
    printf("%s invalid\n", s);
return 0;
}

【问题讨论】:

  • 在 get_month 中,将 months 替换为 array
  • 您的某些字符串末尾有空格,但不是全部。
  • 这段代码甚至无法编译。
  • “找不到”是什么意思?我的编译器显示4: error: two or more data types in declaration specifiers,其中4行号
  • (编辑后)阅读编译器错误信息

标签: c arrays search char


【解决方案1】:
int get_month(const char *str,int char **array)
                               ^

显然你不应该在那里有int,字符串数组最好声明为char*[] 所以应该是

int get_month(const char *str, char *array[])

当你得到索引时,你应该给答案加 1,因为 C 中的索引从 0 开始,但人从 1 开始计数。字符串常量中也有一些空格,这会使 strcmp 失败。


完整的工作代码:

#include <stdio.h>
#include <string.h>
#define SIZE 20
int get_month(const char *str, char **array){

    int k;
    for (k = 0; k < 12; ++k)
        if (!strcmp(array[k], str))
            return k+1 ;
    return 0;
}

int main(){
    char s[SIZE];
    char *months[] = {"January","February","March", "April","May","June","July","August","September", "October","November","December" };
    int result;
    printf("enter the month : ");
    gets(s);
    result = get_month(s,months);
    if (result)
        printf("%s - %d.  is the month of the year.\n", s, result);
    else
        printf("%s invalid\n", s);
    return 0;
}

在这里查看:Coliru Sample

【讨论】:

  • 输入输入月份:十月---->输出:无效@texasbruce
  • @user3718043: 1. october 无效,因为它在您的数组中拼写为“October”。 2. october 无效,因为它前面有一个空格。
【解决方案2】:

首先,现在不推荐使用gets,而是使用fgets。 捕获的string 以换行符终止,因此任何原始比较都将失败。

其次,strcmp 返回 integer,而不是 boolean。并且列表中的一些元素中有空格。

我把它改造成工作程序:

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <ctype.h>

#define MONTHS_ARRAY_LENGTH     (12)

static const char *months[MONTHS_ARRAY_LENGTH] =
{
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August ",
    "September",
    "October",
    "November",
    "December"
};

static int get_month_number(const char *month_mbs)
{
    int month_nb = -1;

    for (int idx = 0; idx < MONTHS_ARRAY_LENGTH; ++idx) {
        if (strcasecmp(months[idx], month_mbs) == 0) {
            month_nb = idx + 1;
            break;
        }
    }
    return (month_nb);
}

int main(void)
{
    int result = 0;
    char s[21] = {0};

    printf("Enter a month: ");

    if (fgets(s, 20, stdin) == NULL) {
        return EXIT_FAILURE;
    }

    /* Get rid of new line character captured by fgets. */
    for (int idx = 0; idx < 21; ++idx) {
        if (!isalpha(s[idx])) {
            s[idx] = '\0';
        }
    }

    result = get_month_number(s);
    if (result < 0) {
        printf("%s is an invalid argument.\n", s);

    } else {
        char a[3] = {0};

        switch (result) {
        case 1:  a[0] = 's'; a[1] = 't'; break;
        case 2:  a[0] = 'n'; a[1] = 'd'; break;
        case 3:  a[0] = 'r'; a[1] = 'd'; break;
        default: a[0] = 't'; a[1] = 'h'; break;
        }

        printf("%s is the %d%s month of the year.\n",
               s, result, a);
    }
    return EXIT_SUCCESS;
}

【讨论】:

  • C 中的整数被用作布尔值,因为没有这样的类型
猜你喜欢
  • 1970-01-01
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 2012-04-13
  • 1970-01-01
相关资源
最近更新 更多