Challenge #1 Code

0 Alex Davenport · October 13, 2014

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef int bool;
#define true 1
#define false 0

int main()
{
    char password[20] = "";
    bool hasUpper = false;
    bool hasNumber = false;
    bool hasDollar = false;
    bool goodLength = false;
    bool goodPassword = false;

    while(goodPassword == false){

        printf("Enter your password\n");
        scanf(" %s", password);

        if(strlen(password) < 8){
            goodLength = true;
            printf("Try another password(too short at least 8 characters)\n");
            continue;
        }else{

            int i = 0;
            for(i = 0; i < strlen(password); i++){
                printf(" %c", password'[i']);
                if(isupper(password'[i'])){
                    hasUpper = true;
                }else if(isdigit(password'[i'])){
                    hasNumber = true;
                }else if(password'[i'] == '$'){
                    hasDollar = true;
                }
            }
            printf("\n");
        }
        if(hasUpper == true && hasNumber == true && hasDollar == true){
            printf("good to go hoss!");
            goodPassword = true;
        }else{
            printf("Try a different password\n");
        }
    }
    return 0;
}

Pretty straightforward challenge #1 code. Note I did use 'typedef' to declare a boolean as a type of integer because this is what I was used to in other languages and it made it way easier. The only other addition was that I added a continue statement to skip the bulk of the loop if the password isn't at least eight characters.

Post a Reply

Replies

Oldest  Newest  Rating
0 Alex Davenport · October 13, 2014
Hmm that's interesting. Why is that happening? I edited the code and added white spaces before and after the '[' and ']' characters and that didn't seem to work, nor did \. For whatever reason adding single quotes worked so if anybody wants to use this just remove the single quotes before the opening and closing brackets.
  • 1

C

107,095 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator