1

我正在尝试创建一个允许用户在文件中搜索名称的程序。该程序成功地做到了这一点,但后来我想到并不是每个人都会输入名称,因为它在文件中是大写的。也就是说,有人可能会搜索“sarah”,但由于该名称在文件中列为“Sarah”,因此将找不到该名称。为了解决这个问题,我尝试在比较时将两个字符串都转换为大写。我对自学 C 非常非常陌生,所以我不确定我是否朝着正确的方向前进。在这一点上,我什至无法编译程序,因为我收到两个错误,说“数组初始化程序必须是初始化程序列表或字符串文字”。我假设这意味着我的语法不仅无效,而且方向完全错误。

     #include <stdio.h>
     #include <ctype.h>
     #include <string.h>

     int main(void)
     {
        FILE *inFile;
        inFile = fopen("workroster.txt", "r");
        char rank[4], gname[20], bname[20], name[20];

        printf("Enter a name: __");
        scanf("%s", name);
        int found = 0;
        while(fscanf(inFile, "%s %s %s", rank, bname, gname)== 3)
        {   char uppername[40] = toupper(name[15]);
            char upperbname[40] = toupper(bname[15]);
            if(strcmp(uppberbname,uppername) == 0)
            {
                printf("%s is number %s on the roster\n", name, rank);
                found = 1;
            }
        }

        if ( !found )
            printf("%s is not on the roster\n", name);

        return 0;

      }
4

4 回答 4

1

这两行是错误的:

char uppername[40] = toupper(name[15]);
char upperbname[40] = toupper(bname[15]);

int toupper(int c); takes an int and returns an int

因为在 C 中字符串只是一个带有空终止符的字符数组,所以你可以做的就是将字符串的每个字符转换为大写:

for (size_t I = 0; I < strlen(name); I++) {
    uppername[I] = toupper(name[I]);
}
uppername[I] = '\0';

关于比较,您可以strcasecmp按照建议使用,即 Posix。如果您只想使用 C stdlib 中的函数,请按上述方式转换字符串,然后使用strcmp.

于 2016-04-19T04:46:27.923 回答
1

toupper()适用于单个字符,而不适用于字符串。

无需转换输入字符串。简单地调用一个字符串不区分大小写的比较。

由于 C 没有标准的,因此很容易创建自己的。

int mystricmp(const char *s1, const char *s2) {
  // toupper works with unsigned char values.
  // It has trouble (UB) with char, when char is signed.
  const unsigned char *p1 = (const unsigned char *) s1;   
  const unsigned char *p2 = (const unsigned char *) s2;   

  while (toupper(*p1) == toupper(*p2) && *p1) {
    p1++;
    p2++;
  }

  int ch1 = toupper(*p1);
  int ch2 = toupper(*p1);
  return (ch1 > ch2) - (ch1 < ch2);
}
于 2016-04-19T15:00:12.337 回答
0

使用以下函数,该函数包含在strings.h

int strcasecmp(const char *s1, const char *s2);

在您的情况下更改 if 语句

if(strcmp(uppberbname,uppername) == 0)

if(strcasecmp(bname,name) == 0)

并删除

char uppername[40] = toupper(name[15]);
char upperbname[40] = toupper(bname[15]);
于 2016-04-19T04:42:11.683 回答
0

因为该函数toupper用于将字符从小写转换为大写,所以不能将其用于字符串大小写转换。但是您可以通过这种方式使用相同的函数进行字符串化:

  while(name[i])
  {
     uppername[i]=toupper(name[i]);
     i++;
  }
  while(bname[j])
  {
     upperbname[j]=toupper(bname[j]);
     j++;
  }

这些语句进行我们的字符串大小写转换。整个节目:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void) {
  FILE *inFile;
  inFile = fopen("workroster.txt", "r");
  char rank[4], gname[20], bname[20], name[20], uppername[40], upperbname[40];
  printf("Enter a name: __");
  scanf("%s", name);
  int found = 0, i = 0, j = 0;

  while (fscanf(inFile, "%s %s %s", rank, bname, gname) == 3) {
    while (name[i]) {
      uppername[i] = toupper(name[i]);
      i++;
    }
    while (bname[j]) {
      upperbname[j] = toupper(bname[j]);
      j++;
    }

    //char uppername[40] = toupper(name[15]);
    //char upperbname[40] = toupper(bname[15]);
    if (strcmp(uppername, upperbname) == 0) {
      printf("%s is number %s on the roster\n", name, rank);
      found = 1;
    }
  }

  if (!found) printf("%s is not on the roster\n", name);
  return 0;
}
于 2016-04-19T04:46:05.487 回答