C Challenge #1

+2 Steven vd_W · December 13, 2014
Hello!
I've made the first challenge and I now wonder what you guys/ girls think about it!
(It does work good for me!)

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

int main()
{

    char ansUser[20];
    int poCheck;
    int upCase=1, num=1, dollar=1, strong=1;

    printf("Please enter a password, \nInclude at least 1 capital letter, 1 number and a $ sign \nand make it equal to or more than 8 characters: ");
    scanf(" %s", &ansUser);

    for(poCheck=0; poCheck <= strlen(ansUser); poCheck++) {

        if( isalpha(ansUser[poCheck])) {
            if( isupper(ansUser[poCheck])) {
                upCase++;
            }
        }
        if( isdigit(ansUser[poCheck]) ){
            num++;
        }
        if( ansUser[poCheck] == '$' ) {
            dollar++;
        }
        if( strlen(ansUser) >= 8) {
            strong++;
        }
    }

    if( (upCase>1) && (num>1) && (dollar>1) && (strong>1) ) {
        printf("Your password is good!");
    } else {
        printf("Invalid input:\n");
        if( upCase==1 ) {
            printf("You forgot to include at least 1 upper case letter\n");
        }
        if( num==1 ) {
            printf("You forgot to include at least 1 number\n");
        }
        if( dollar==1 ) {
            printf("You forgot to include at least 1 dollar sign\n");
        }
        if( strong==1 ) {
            printf("Your password isn't more or equal to 8 characters");
        }
    }




    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Steven vd_W · December 14, 2014
As I actually said, it will not work if you keep the code as it now with this: http://i.gyazo.com/a2a986923364f1c0aa4d1764b38f6134.png
Because you can't store more than 20 characters in the 'ansUser', I don't know yet how to put a maximum characters on the password but if you change this: http://i.gyazo.com/a2a986923364f1c0aa4d1764b38f6134.png to something like this: http://i.gyazo.com/dbcaba569953f37c6487e24524e1bea4.png it will work. and if I know how to make a maximum limit of characters I will add that and it will work even easier and better.
http://i.gyazo.com/a77e2c0f0a92822d26a6a105e91f096f.png
0 c student · December 14, 2014
/images/forum/upload/2014-12-14/0822fec29444996180e3f8bab897ae55.png
:'(
0 Steven vd_W · December 14, 2014
I just tested it, if you let ansUser only gold 20 characters it will say: Your password is good. But if you let it hold more than 20 characters, it works just fine!
0 Steven vd_W · December 13, 2014
@cstudent The 'ansUser' van now only hold 20 characters but you can make it as much as you want, and if it can hold 100 or more characters it would say to your password: 
---
Invalid input:
You forgot to include at least 1 capital letter
You forgot to include at least 1 nummer
You forgot to include at least ons dollar sign
----
You can always test it, and copy or paste the program in codeblock or something
0 c student · December 13, 2014
i'm just a bit curious here, what would happen if you typed in something with a length greater than 20... much greater, without any of those required characters, say, "ifitypeinthispassworditmightjustaccidentallybreaksomethinginsideyourprogram"??  can you tell me what happens?? :ermm:
  • 1

C

107,254 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator