Search code examples
cstringpointersstring-comparison

Compare password to a given set of passwords


I have got an assignment from uni which mentions that I have to compare a given set of passwords to the password given by the user. The set of passwords are predetermined in the question as follows

const char *passwd[NUM_PASSWDS] = {
    "123foo", "bar456", "bla_blubb"
};

and has to be compared with input from user...

So I have written my code as follows;

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

#define NUM_PASSWDS 3

const char *passwd[NUM_PASSWDS] = {
    "123foo", "bar456", "bla_blubb"
};

int pwdquery(char pass[]) {
    for (int i = 0; i < 2; i++) {
        if (passwd[i] == pass[i]) {
            return printf("correct");
        }
    }
}

int main() {
    char a[100];
    for (int i = 0; i < 3; i++) {
        printf("Please enter password");
        scanf("%s", a);
    }
    pwdquery(a);
}

When I tried running my code, it shows an error...

Thank you for your time


Solution

  • To compare string you need to strcmp function.
    printf returns number of character printed. You should return fixed value on success or failure of password match. In my example I'm returning 1 in case of password match or 0 in case of no-password match.
    In main function scanf function with %s argument can read entire string. You don't need for-loop to read input string.

    #include<stdio.h>
    #include<string.h>
    #define NUM_PASSWDS 3
    
    const char* passwd[NUM_PASSWDS] = {
    "123foo", "bar456", "bla_blubb"
    };
    int pwdquery(char pass[]) {
        for (int i = 0; i < NUM_PASSWDS; i++) 
        {
            if (strcmp(pass, passwd[i]) == 0) 
            {
                return 1; // One means currect password
            }
        }
        return 0; // Zero means incorrect password password
    }
    
    int main() {
        char a[100];
        printf("Please enter password");
        scanf("%s", a);
    
        if(pwdquery(a) == 1)
        {
            printf("Password entered is correct");
        }
        else
        {
            printf("Password entered is incorrect");
        }
    }
    

    With custom strcmp

    #include<stdio.h>
    #include<string.h>
    #define NUM_PASSWDS 3
    
    const char* passwd[NUM_PASSWDS] = {
    "123foo", "bar456", "bla_blubb"
    };
    
    //Returns 0 if string don't match else returns 1
    int user_strcmp(const char *str1, const char *str2)
    {
        size_t str1_size = strlen(str1);
        size_t str2_size = strlen(str2);
        if(str1_size != str2_size)
        {
            return 0; //String are not of same size. The strings won't match.
        }
        for(size_t i = 0; i < str1_size; i++)
        {
            if(str1[i] != str2[i])
            {
                return 0;
            }
        }
        return 1;
    }
    int pwdquery(char pass[]) {
        for (int i = 0; i < NUM_PASSWDS; i++) 
        {
            if (user_strcmp(pass, passwd[i]) == 1) 
            {
                return 1; // One means currect password
            }
        }
        return 0; // Zero means incorrect password password
    }
    
    int main() {
        char a[100];
        printf("Please enter password");
        scanf("%99s", a);
    
        if(pwdquery(a) == 1)
        {
            printf("Password entered is correct");
        }
        else
        {
            printf("Password entered is incorrect");
        }
    }