如何检查输入的字符串是否已存在于C中 [英] How to check if string entered already exists in C

查看:110
本文介绍了如何检查输入的字符串是否已存在于C中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑是否创建了一个函数来检查用户输入的字符串是否已存在于结构中。



我有以下结构存储汽车数据:



I am here with a doubt to create a function to check if a string entered by the user already exists in a struct.

I have the following struct to store cars data:

typedef struct
    {
        char brand;
        char car_plate[10];
        char model;
    } stCar cars;





例如当被要求输入car_plate时,我需要一个函数来检查car_plate是否已经存在于stCar结构中,因为不能存在两辆具有相同car_plate的汽车。





我不知道这是否有效:





For example when asked to enter the car_plate, i need a function to check if that car_plate already exists in the stCar struct because can not exist two cars with the same car_plate.


I don't know if this will work:

int main()
{
  stCar cars;
  int countCars =0;










void entercar(stCars cars[], int *countCars)
   {

       char car_plate_Aux[10];
       int position;
           do
           {

               printf("\nEnter car plate:");
               fgets(car_plate_Aux);
               findcar_plate(cars,*countCars, car_plate_Aux)
             if (position != -1)
             {
               printf("This car already exists!\n");
           }
       }
       while(position != -1);

       cars[*countcars].car_plate =car_plate_Aux;
   }













char findcar_plate(stCars cars[], int countCars, char car_platetofind)
    {
        int i, position;
    
        position = -1;
    
        for(i = 0 ; i < coutCars ; i++)
        {
            if (cars[i].car_plate == car_platetofind)
            {
                position=i;
                i=countCars;
            }
        }
        return position;





有人可以帮我吗?



Someone can please help me?

推荐答案

由于字符串长度只有10,所以不值得使用hash,尝试比较字符串到字符串。

而不是使用

since the length of string only 10 , it is not worth to use hash, try to compare string to string.
instead of using
if (cars[i].car_plate == car_platetofind)





使用strcmp比较2字符串。



分配car_plate时的最后一个注释(与安全相关)请确保只分配10个字符而不是更多。



use strcmp to compare the 2 string.

last note (related to security) when you assign the car_plate be sure that you assign only 10 chars not more.


由于几个原因,这不会起作用。

如前所述,你无法比较字符串,你必须使用 strcmp [ ^ ](或strncmp而不是。

你永远不会为汽车数组的项目分配内存(你甚至没有将它声明为数组)。

如前所述,你没有检查用户输入(它必须适合数组)。

That wouldn't work for several reasons.
As already noted you cannot compare strings that way, you have to use strcmp[^] (or strncmp) instead.
You never allocate memory for items of the cars array (you didn't even declare it as an array).
As already noted you didn't check the user input (it must fit in the array).
引用:

cars [* countcars] .car_plate = car_plate_Aux

cars[*countcars].car_plate =car_plate_Aux

在这里,您要设置一个不存在的数组项,其中包含临时变量的地址(您应该复制该数组)。

...

Here you are setting a not-existing array item with the address of a temporary variable (you should have copied the array).
...


这篇关于如何检查输入的字符串是否已存在于C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆