【问题标题】:How to read string in structure with space? [duplicate]如何读取带有空格的结构中的字符串? [复制]
【发布时间】:2017-08-07 12:17:17
【问题描述】:

我们的公司办公室需要一个应用程序来维护钦奈的所有注册大学,并且该应用程序在搜索大学方面应该是用户友好的。使用以下属性创建一个名为“大学”的结构:名称、许可证号和区号。

要求:大学执照号码为6位,前2位为大写字母,后4位为数字。

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

struct University
{
  char name[100];
  char license[10];
  int area;

}u[10];

void main()
{
  int i, n, r, k = 0, flag = 1, f2 = 1, j, search = 0;
  char s[100];
  printf("Enter the number of records\n");
  scanf("%d", &n);
  printf("Enter the details of %d universities\n", n);

  for (i = 0; i<n; i++)
  {
    printf("Name of the University\n");
    getchar();
    scanf("%s", u[i].name);
    j = strlen(u[i].name);
    if (j <= 1)
    {
      f2 = 0;
      break;
    }
    printf("License Number\n");

    scanf("%s", u[i].license);
    k = strlen(u[i].license);
    if (k<1)
    {
      f2 = 0;
      break;
    }

    if (k<6)
    {
      flag = 0;
    }
    else if ((u[i].license[0] >= 'A' && u[i].license[0] <= 'Z') && (u[i].license[1] >= 'A' && u[i].license[1] <= 'Z') && (u[i].license[2] >= '0' && u[i].license[2] <= '9') && (u[i].license[3] >= '0' && u[i].license[3] <= '9') && (u[i].license[4] >= '0' && u[i].license[4] <= '9') && (u[i].license[5] >= '0' && u[i].license[5] <= '9') && k == 6)
    {
      flag = 1;
    }
    else
    {
      flag = 0;
    }
    printf("Area Code\n");
    scanf("%d", &u[i].area);
    //printf("%d",u[i].area);
    if (u[i].area <= 0)
    {
      f2 = 0;
    }

  }
  if (flag == 0)
  {
    printf("Sorry! You have entered incorrect license number.");
  }
  else if (f2 == 0)
  {
    printf("Unable to continue");
  }
  else
  {
    printf("Enter the name of the University to be searched\n");
    scanf("%s", s);
    for (i = 0; i<n; i++)
    {
      if ((strcmp(u[i].name, s)) == 0)
      {
        search = 1;
      }
    }
    if (search == 1)
    {
      printf("University is licensed one.");
    }
    else
    {
      printf("University is not found.");
    }
  }
}

当我给大学的编号为 3 时,它不接受第三大学的输入。

测试用例

输入 1

输入记录数

3

输入3所大学的详细信息

大学名称

SRM

许可证号

SR1234

区号

28

大学名称

马德拉斯大学

许可证号

SP0904

区号

18

大学名称

巴拉特大学

许可证号

BU0101

区号

35

输入要搜索的大学名称

SRM

输出 1

大学是获得许可的。

【问题讨论】:

  • 请展示一些输入和预期/实际输出的示例。
  • 整个程序逻辑错误,过于复杂。
  • 使用scanf 读取用户输入的用途有限,因为它忽略了换行符。它需要更多的错误检查。特别是,如果下一个标记不是数字,%d 格式将不会消耗流,而%s 格式只会读取到下一个空格的字符串。如果您输入的是“New Delhi”、“AA9876”和“123”,则名称为“New”,许可证为“Delhi”,根本不会读取该区域。
  • 丢掉所有的标志,改用正则表达式。
  • 告诉我如何读取结构化数组中带空格的字符串,因为我多次遇到这个问题

标签: c struct


【解决方案1】:

似乎您只对读取包含空格的 c 字符串感兴趣。为此,您可以使用fgets。这是一个玩具程序:

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

struct s {
    char name[100];
    int something;
};

int main(void)
{
    struct s myStruct;
    printf("%s", "Enter name: ");
    fgets(myStruct.name, 100, stdin);

    myStruct.name[strlen(myStruct.name) - 1] = '\0'; //This should remove the newline char at the end
    printf("Name is: %s", myStruct.name);
}

【讨论】:

  • 次要:myStruct.name[strlen(myStruct.name) - 1] 允许通过在 fgets() 调用中输入 null 字符 作为第一个字符来进行黑客攻击。为避免,请测试 myStruct.name[0] == '\0') 或仅使用 myStruct.name[strcspn(myStruct.name,"\n")] = '\0';
  • @chux 感谢您指出这一点。我还在学习。
  • 顺便说一句:考虑删除“您的程序过于复杂且过于复杂”。因为它不会为这个答案增加客观价值。
  • 我也用过..但它也会产生同样的错误
  • @PrakharDubey 什么错误?复制粘贴我的玩具程序,看看是否出现“错误”。
猜你喜欢
  • 1970-01-01
  • 2011-09-11
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多