Search code examples
csubstringc-stringsfunction-definition

How does this function that verify if a informed name is equal to other name works


I've been looking for codes in C that verify if two strings are equal. I found this one but I don't understand the logic in the for and the while parts.

int verify(phonebook name[],char name_verify[])
{
    int i,k;

    char *p,temp[strlen(name_verify)];

    if(strlen(name) >= strlen(name_verify))
    {
        for(i=0; i<=(strlen(name) - strlen(name_verify)) ; i++)
        {
            p=&name[i];
            k=0;
            
            while(k!=strlen(name_verify))
            {
                temp[k] =* (p + k);
                k++;
            }
            temp[k]='\0';

            if(strcmp(strupr(temp),strupr(name_verify))==0)
            {
                return 1;
            }
        }
    }

Could someone explain to me how it works?


Solution

  • For starters the declaration of the function

    int verify(phonebook name[],char name_verify[])
    

    confusing because it is not clear what the type specifier phonebook means. So the reader of the function needs to investigate the function body to assume that phonebook is a synonym for the type char.

    This declaration of a variable length array

    char *p,temp[strlen(name_verify)];
    

    can be a reason of undefined behavior because it does not have a space to store the terminating zero character '\0' of the string name_verify.

    You have to write

    char *p,temp[strlen(name_verify) + 1];
    

    But using a variable length array can be a reason of a stack overflow. Using this array is entirely redundant. The function can be written without it.

    Within the function there are numerous redundant calls of the function strlen.

    The function tries to check whether the string name contains within itself a substring that is equal to the string name_verify if to convert the both to upper case.

    At first the function checks that the length of the string name is not less than the length of the string name_verify.

    if(strlen(name) >= strlen(name_verify))
    

    Then in the for loop

    for(i=0; i<=(strlen(name) - strlen(name_verify)) ; i++)
    

    it starts to traverse the string name and determine whether the substring starting from the index i in the string name is equal to the string name_verify.

    For this purpose the substring of the string name is copied in the array temp

            p=&name[i];
            k=0;
            
            while(k!=strlen(name_verify))
            {
                temp[k] =* (p + k);
                k++;
            }
            temp[k]='\0';
    

    After the copy is finished the formed string in the array temp is compared in upper case with the string name_verify.

            if(strcmp(strupr(temp),strupr(name_verify))==0)
            {
                return 1;
            }
    

    If they are equal the function return 1.

    Otherwise if there is no such a substring in the string name that is equal to name_verify it seems the function returns 0 in a return statement that is not shown in the provided code.:)