如何用空格读取结构中的字符串?

时间:2017-08-07 12:17:17

标签: c struct

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

要求:大学的许可证号码应为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时,它没有接受第3所大学的投入。

测试用例

输入1

输入记录数

3

输入3所大学的详细信息

大学名称

SRM

许可证编号

SR1234

区号

28

大学名称

马德拉斯大学

许可证编号

SP0904

区号

18

大学名称

巴拉特大学

许可证编号

BU0101

区号

35

输入要搜索的大学的名称

SRM

输出1

大学获得许可。

1 个答案:

答案 0 :(得分:1)

似乎您只想阅读包含空格的c字符串。为此,您可以使用// given els is one of the elements extracted from this object: let sorted = els.sort((a, b) => a.distance.value - b.distance.value); 。这是一个玩具程序:

<template>
  <v-container>
    <h4>name: {{anObject.name}}</h4>
  </v-container>
</template>

<script>
  import Vue from 'vue'
  import db from '@/js/firebase.js'

  export default {
    props: ['id'],
    firebase: {
      anObject: {
        source: db.ref(this.getPath),
        //source: db.ref('path/id'),
        asObject: true,
        cancelCallback: function () {console.log("Cancel")},
        readyCallback: function () {console.log("Ready")}
     },
   computed: {
     getPath () {
       return 'path/' + this.id
     }
   }
 </script>