1
\$\begingroup\$

I wrote this simple program to compare two strings ,I think it's working properly I just need to confirm that and also have few advices if possible

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

int main(){

    int value, lenght=0, i=0;
    char passwd[11], pass[11]="comptiatar";

    printf("enter ur passwd: ");
    scanf("%s", passwd);

    //value = strcmp(passwd,pass);

    while (passwd[i]!=0){
        lenght++;
        i++;
    }
    
    
    for (i=0;i<=lenght;i++){
        if (passwd[i]==pass[i])
            value=0;
        else {
            value=passwd[i]-pass[i];
            break;
        }
    }
    
    if (value)
        printf("\nwrong password\n\n");
    else
        printf("\nlogin successfully\n\n");
    printf("%d\n", value);

return 0;}

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Is it really necesary to "reinvent the wheel"? Instead of this:

while (passwd[i]!=0){
        lenght++;
        i++;
    }

could just use:

lenght=strlen(passwd);

And what the problem with just using strcmp? Also you can exit the for loop as soon passwd[i]!=pass[i].

Also what hapens if somebody type password longer then 10 characters? You should make passwd biger or check the return value of scanf.

\$\endgroup\$
2
  • \$\begingroup\$ I though it will be good if I create my own function \$\endgroup\$ Commented Feb 5, 2022 at 17:31
  • \$\begingroup\$ @UnderScore Not realy, specially your implementation has no advantages. \$\endgroup\$ Commented Feb 5, 2022 at 17:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.