将字符串转换为大写以与C中的用户输入进行比较

将字符串转换为大写以与C中的用户输入进行比较,c,arrays,strcmp,toupper,C,Arrays,Strcmp,Toupper,我正在尝试创建一个程序,允许用户在文件中搜索名称。程序成功地做到了这一点,但我突然想到,并不是每个人都会在文件中键入大写的名称。也就是说,有人可能会搜索“sarah”,但由于该名称在文件中被列为“sarah”,因此将找不到该名称。为了解决这个问题,我尝试在比较时将两个字符串都转换为大写。我对自学C语言非常陌生,所以我不确定自己是否朝着正确的方向前进。在这一点上,我甚至无法让程序编译,因为我得到了两个错误,即“数组初始值设定项必须是初始值设定项列表或字符串文字。”我假设这意味着我的语法不仅无效,而

我正在尝试创建一个程序,允许用户在文件中搜索名称。程序成功地做到了这一点,但我突然想到,并不是每个人都会在文件中键入大写的名称。也就是说,有人可能会搜索“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;

      }
#包括
#包括
#包括
内部主(空)
{
文件*填充;
infle=fopen(“workfloster.txt”,“r”);
char-rank[4],gname[20],bname[20],name[20];
printf(“输入名称:”;
scanf(“%s”,名称);
int=0;
而(fscanf(infle,“%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是花名册上的编号%s\n”、姓名、级别);
发现=1;
}
}
如果(!找到)
printf(“%s不在花名册上\n”,名称);
返回0;
}

使用
strings.h

int strcasecmp(const char *s1, const char *s2);
在您的情况下,更改if语句

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

删除

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

由于函数
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;
}
#包括
#包括
#包括
内部主(空){
文件*填充;
infle=fopen(“workfloster.txt”,“r”);
char-rank[4],gname[20],bname[20],name[20],uppername[40],upperbname[40];
printf(“输入名称:”;
scanf(“%s”,名称);
int=0,i=0,j=0;
而(fscanf(infle,“%s%s%s”,rank,bname,gname)=3){
while(名称[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是花名册上的编号%s\n”、姓名、级别);
发现=1;
}
}
如果(!found)printf(“%s不在花名册上\n”,名称);
返回0;
}

这两行是错误的:

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';
for(大小I=0;I
关于比较,您可以按照建议使用
strcasecmp
,即Posix。 如果只想在C stdlib中使用函数,请如上所述转换字符串,然后使用
strcmp

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);
}
intmystricmp(常量字符*s1,常量字符*s2){
//toupper使用无符号字符值。
//当char被签名时,它与char(UB)有问题。
常量无符号字符*p1=(常量无符号字符*)s1;
常量无符号字符*p2=(常量无符号字符*)s2;
而(toupper(*p1)=toupper(*p2)和&*p1){
p1++;
p2++;
}
int ch1=toupper(*p1);
int ch2=toupper(*p1);
返回(ch1>ch2)-(ch1
一个很好的起点是学习您正在调用的api的实际功能<例如,code>toupper
返回单个
char
,因此不适合初始化
char
数组。顺便说一句,如果你不介意把你的马车挂到posix拖拉机上,可能会使这项任务更容易(或在Windoze中)。确保你验证并限制所有输入。e、 g.
if(scanf(“%19[^\n]]*c”,name)!=1{fprintf(stderr,“error:invalid input.\n”);返回1;}
而不仅仅是从文件中读取的输入。如果用户使用+恶意+命令%%输入
someverylongstring\uuuu%%怎么办?注意:
strcasecmp()
不在标准C库中。@user3302647
strcasecmp()
不在
uppername[]
中,并且
upperbname[j]
缺少空字符。因此,这只是说我正在搜索的名字都找不到。
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);
}