Challenge 1 completed

+4 Горан Куга · October 13, 2014
Hi guys. I'm done with challenge 1 waiting for your suggestions on my code. I also provide same code but with comments http://pastebin.com/qYzvMmuZ.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

/*
Challenge #1 Buckys Room
Is password strong or weak by Goran
*/

int main()
{
int upperCase = 0, number = 0, dolarSign = 0, loop;
char password[20], temp;
printf("Please enter your password: ");
scanf(" %s", password);

for(loop=0; loop<=20; loop++){
temp = password[loop];
if(isupper(temp)){
upperCase++;
}if(isdigit(temp)){
number++;
}if(temp == '$'){
dolarSign++;
}

}if(upperCase >= 1 && number >= 1 && dolarSign >= 1){
printf("Your password is strong. \n \n");
}else{
printf("Your password is weak. \n \n");
}

system("pause");
return 0;
}

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Горан Куга · October 18, 2014
Thanks. I used loop that starts at 0 and ends at 20 because at the  beginning i told that user is able to enter password up to 20 characters. Let's say our password is ImGood25$ this loop is gonna check one character at the time and  assign correct value to our int's upperCase, number and dolarSign. Check comments in code.
	for(loop=0; loop<=20; loop++){ // loop that will start at 0, end at 20 and will increment by one.
temp = password[loop]; // storing one character from user password at a time in char temp for testing.
if(isupper(temp)){ // if character that we are testing is uppercase letter.
upperCase++; // increment upperCase integer by one if statement above is true.
}if(isdigit(temp)){ // if character that we are testing is number.
number++; // increment number integer by one if statement above is true.
}if(temp == '$'){ // if character that we are testing is dolar sign.
dolarSign++; // increment dolarSign integer by one if statement above is true.
}
+1 AHafiz Suara · October 20, 2014
Goran, I think your code is fantastic and I also think there is a bug. Your array password[20] can only store 20 elements i.e. from 0 to 19...your loop is probably going to fall into the Segfault. I like the way you name your variables though.

Ayink.., you for loop will only check for the first char in the string always - in my opinion...you did not make use of the i counter at all...I wonder if you were able to make it work.
+1 Daniel Bogorad · October 28, 2014

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


int main()
{
    int a;
    int upperCase = 0 , numberChar = 0 , dollarSign = 0 ;
    char password[10] ;
    printf("Enter a 10 char password with one capital , one number , and a $ sign \n.");
    scanf(" %s", password);

    for (a=0 ; a<=10 ; a++){
        if(isalpha(password[a])){
            if(isupper(password[a])){
                upperCase++;
            }
        }else if(isdigit(password[a])){
            numberChar++;
        }else if(password[a] == '$'){
            dollarSign++;
        }
    }


    if ((upperCase >=1) && (upperCase >= 1) && (dollarSign >= 1)){
        printf("\n Your password is strong enough . \n");
    }else {
       printf("Too weak , please try again \n");
    }

    printf(" %d , %d , %d", upperCase, numberChar, dollarSign); /* just to check for myself when i was making the program -
                                                                    if it reads all the required symbols , and adds one on the
                                                                    presence of one of them . */
}


hy ! :)  so that's my version of the code, looks like it works and it is actually similar to the solutions above .. 
and a small question - in Goran K's code - Why use a "temp" variable and assign the password to it ?? I think its not neccessary ? - if someone could explain i'd be glad :)

thanks ))
+1 Anthony B · October 18, 2014
Goran great code man.

 But why the loop?

for(loop=0; loop<=20; loop++){
       temp = password[loop];
        if(isalpha(temp) && isupper(temp)){
           upperCase++;
       }if(isdigit(temp)){
           number++;
       }if(temp == '$'){
           dolarSign++;
0 Горан Куга · October 25, 2014

No, you have to loop until you encounter first null terminating character. And for extra protection you should loop only sizeof(password) - 1 times.


I'm kinda new in C Programming. Can you fix my code with loop sizeof(password) - 1 ? Thanks.
0 Горан Куга · October 25, 2014
So i should end loop at 19 ?
for(loop=0; loop<=19; loop++)
0 Горан Куга · October 20, 2014

Goran, I think your code is fantastic and I also think there is a bug. Your array password[20] can only store 20 elements i.e. from 0 to 19...your loop is probably going to fall into the Segfault. I like the way you name your variables though.


Hey buddy. Thanks i'm glad it looks good. I didn't quite understood what you wrote if you could maybe explain what you mean.
0 Горан Куга · October 21, 2014
So to fix it i should end my loop at 19 instead of 20 ?
0 Ass Cold · October 29, 2014
How come bucky never reply... in every video, he say he will help you guys out...
0 Daniel Bogorad · October 30, 2014
hy guys :)

so , Krootushas , i think i understood what you meant , 
with that said , i made the following change if the main loop :
 ** sizeof instead of just 10 .
for (a=0 ; a<sizeof(password)-1 ; a++){ // loop how many times the users password character count is .
        if(isalpha(password[a])){ // check if it is a letter
            if(isupper(password[a])){ // if it is a letter than check if its a CAPITAL LETTER
                upperCase++; // register the capital
            }
        }else if(isdigit(password[a])){ // Is this a number ?
            numberChar++; // register the number
        }else if(password[a] == '$'){ // Is it a $ sign ?
            dollarSign++; // register sign
        }
    }

looks like there is no difference though //
  • 1
  • 2

C

107,261 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator