C challenge #1, string compare crash and problems with too many characters

+1 Daniel Arnlund · December 21, 2014
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char password[8];
    int i, digit = 0, upper=0, dollar=0;
    char test;

    while (1) {                             //test loop
        puts("Type in your password:");
        fgets(password, 8, stdin);

       /* if (strncmp(password, 'quit', 4) == 0 ) {  //quitting the loop
        puts("Breaking...");
        break;
    }                                   string compare makes the program crash */

        for (i=0; i<8; i++){
            test = password;

                if (isupper(test)==1) {     // tested with and without "==1"
                upper++;
                }
                else if(isdigit(test) == 1) {
                    digit++;
                }
                else
                    if (test == '$'){
                        dollar++;
                    }
        }

        if ( upper >= 1 && digit >= 1 && dollar >= 1){
        puts("Nice secure password you got there");
        digit = 0;
        upper = 0;
        dollar = 0;
        }

    else puts ("My granny has a better password, try again");
    }


    return 0;
}


The issues I've found are these;
If I type in more than 8 characters the fail message "My granny has a better password, try again" prints a few times. Why?
strncmp makes the whole thing crash. 

Post a Reply

Replies

Oldest  Newest  Rating
+1 c student · December 21, 2014
some pre-requisite information, gets reads in characters until the newline character or until EOF.  for the following explanation, the newline character is '\n'.
/images/forum/upload/2014-12-21/2af9d886066a0cbb7eb07f72a772eb94.png

the string "111111112222" has 12 characters, none of which are newline characters.  fgets first reads in the first 8 bytes as you requested:
fgets(password, 8, stdin);

when that was done, it was re-read again by fgets as a consequence of the password buffer overflowing with the extra characters i gave in due to the lack of the newline or EOF character.  the new value of password after being re-read now contains a '\n' character.
/images/forum/upload/2014-12-21/10b060fda7f3fd2cead3f6bb53f8ee70.png
fgets now terminates correctly because of the '\n' character.

some extra information:
your else statement at the bottom does not have a corresponding if statement (since you commented it out from the code).  your strncmp error was probably not detected by whatever compiler you used due to the dismissed warning flags (i assume you use codeblocks?).  my gcc detected that your strncmp string 'quit' should be "quit" as ' ' is for characters and " " is for strings.
(strncmp(password, 'quit', 4) == 0 );   // should be "quit" here

good luck.
0 Daniel Arnlund · December 22, 2014
Thank you for your help.

I think I have made it better, and the over flow shouldn't be a problem.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

int main()
{


    int i, digit = 0, upper=0, dollar=0, count=0;
    char test;

    char password[8];


    while ( 1 ) {                             //test loop

        puts("Type in your password:");
        gets(password);
        puts(password);

       if (strncmp(password, "quit", 4) == 0 ) {  //quitting the loop
            puts("Breaking...");
            break;
        }

        for (i=0; i<strlen(password); i++){
            test = password;
            count++;




                if (isupper(test)==1) {     // tested with and without "==1"
                upper++;
                }
                else if(isdigit(test) == 1) {
                    digit++;
                }
                else if (test == '$') {
                        dollar++;
                }


        }

        if ( count > 8) {
                   puts("Your password is too long, try a new shorter one");
                    digit = 0;
                    upper = 0;
                    dollar = 0;
                    count = 0;
                    break;
        }
        else if ( upper >= 1 && digit >= 1 && dollar >= 1){
        puts("Nice secure password you got there");
        digit = 0;
        upper = 0;
        dollar = 0;
        count = 0;
        }

        else {
                puts ( "My granny has a better password, try again.\n" ) ;
                digit = 0;
                upper = 0;
                dollar = 0;
                count = 0;
        }

        }

    return 0;
}
0 c student · December 22, 2014
if you compiler did not warn gets() as deprecated, just be careful with it since it isn't particularly "safe" for use.  fgets() would have been fine with another function to "absorb" the rest of the buffer.
  • 1

C

107,102 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator