Home > Software design >  How does this function that verify if a informed name is equal to other name works
How does this function that verify if a informed name is equal to other name works

Time:09-24

So i've been looking for codes in C that verify if two strings are equal. I found this one and i didnt undertand the logic in the FOR and the WHILE part

    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?

CodePudding user response:

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.:)

CodePudding user response:

With respect to "This is a bad code", I'd say that the main reason is that it is unnecessarily complex for what it does, so much so that it I could believe that it has been intentionally obfuscated, and it does not make use of the obvious standard library functions that would apply. To demonstrate, here is one way I might write the function:

int verify(const char name[], char name_verify[]) {
    char temp = strdup(name);
    int result = ((strstr(strupr(temp), strupr(name_verify))) != NULL);

    free(temp);
    return result ;
}

That's (so much!) cleaner and clearer.

If you happen not to have strdup() (which is specified by POSIX, not the C language specification) then you can always implement your own:

char *strdup(const char *src) {
    char *result = malloc(strlen(src)   1);

    if (result) {
        strcpy(result, src);
    }

    return result;
}
  • Related