Challenge#1 example in C

0 Mike Karavan · May 15, 2015
This is my solution to the challenge.  I have added some commentary to assist with the flow of how the program works.  

I know that it's not the most efficient production as I have only been working with C for a few days, but it does everything that is asked.  

Hope it helps!




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

//This is a simple program designed to simulate how a password validator would work.
//It checks to make sure that the password contains at least 1 capital letter,
//at least 1 digit, and contains the dolar ($) sign.


//The program works as follows:
//First, a variable named password is declared with a maximum set of 20 characters.
//The user is then prompted to enter the password from the print and scan methods at
//the top of main.

//After that, the size of the password is obtained and an int variable named conditionsPasses
//is declared.  This variable acts sort of like a gateway to check all the conditionals in the program.


//FIRST LOOP :
//go through all of the characters individually to check if a capital letter exists.  If it doesn't, then conditions passed does not increment
//and the program will end.

//SECOND LOOP:
//only operates if conditionsPasses is at 1, or has passed the capital letter conditional.  conditionsPassed increments to 2 if a digit
//exists within the password, program ends if it does not.

//THIRD LOOP:
//Only reached if the first 2 conditions were met.  If a dollar ($) sign exists, then the user is prompted that the password is acceptable.

//NOTE, the condition password == '\0' is placed to signal that the loop should cease if an empty character space is reached.
int main(){

    char password[20];                    

    printf("Type your password here: ");
    scanf(" %s", password);

    int sizeOfPassword = sizeof(password);
    
    int conditionsPassed = 0;


    for(int i = 0; i < sizeOfPassword; i++){

        if( (password >= 'A' ) && (password <= 'Z') ){

            ++conditionsPassed;
            break;

        } else {

            
            if(password == '\0'){

                break;

            }

    
        }
    }


    if(conditionsPassed == 1) {

        for(int i = 0; i < sizeOfPassword; i++){

            if( isdigit(password)  ) {

                ++conditionsPassed;
                break;

            } else {

                if(password == '\0'){

                                    break;

                            }
            }

        }


    }


    if(conditionsPassed == 2){

        for(int i = 0; i < sizeOfPassword; i++){

            
            if(password == '$'){

                ++conditionsPassed;
                break;
            
            } else {


                if(password == '\0'){

                                        break;

                                }

            }

        }
    }

    if(conditionsPassed == 3){

        printf("All conditions passed.  Congratulations, your password is acceptable. \n");


    } else {

        printf("Sorry, the password you entered is not acceptable.\n");


    }




    



    return 0;
}




Post a Reply

Replies

Oldest  Newest  Rating
0 Chifundo Ganiza · May 30, 2015
Good work...
0 rasen itachi · June 2, 2015
Thanks bro
0 Suraj Gautam · June 2, 2015
rasen bro
you gained your skills from bucky's video or you are expert? 
I am in challenge 1.... I know all the concept of bucky's tutorials but I can't solve this challenge. 
  • 1

C

107,340 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator