strcmp with pointers not working in C

前端 未结 6 1318
情话喂你
情话喂你 2020-12-20 17:39

Why this code isn\'t working. Just trying to check if the user input is the same as a password

char *pass;

printf(\"Write the password: \");
scanf(\"%s\", p         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-20 18:30

    #include
    main()
    {
        int mystrcmp(char *,char *);
    
        char s1[100],s2[100];
        char *p1,*p2;
        p1=s1;
        p2=s2;
        printf("Enter the first string..?\n");
        scanf("%s",p1);
        printf("Enter the second string..?\n");
        scanf("%s",p2);
        int x=mystrcmp(p1,p2);
        if(x==0)
            printf("Strings are same\n");
        else
            printf("Strings are not same..\n");
    
    
    }
    int mystrcmp(char *p1,char *p2)
    {
        while(*p1==*p2)
        {
            if(*p1=='\0' || *p2=='\0')
                break;
            p1++;
            p2++;
        }
        if(*p1=='\0' &&as *p2=='\0')
            return(0);
        else
            return(1);
    }
    

    simple code for beginners....

提交回复
热议问题