C programing - Challenge #1

+4 Matt Thomson · November 19, 2014
Hi guys,

This is my first post here, I'm to sharing my code for challenge 1. This works well, and is able to tell you which criteria the password failed. 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{

    char password[21];
    int check = 0;
    int capital = 0, number = 0, dollar = 0;

    printf("This program will check the strength of your password.\nPlease enter your password (this is totally not a scam): ");
    scanf("%s", &password);
    printf(" %s\n", password);

    for(check=0; check<=20; check++){
        if( isupper(password[check]) ){
            capital = 1;
        }
        if( isdigit(password[check]) ){
            number = 1;
        }
        if( password[check] == '$' ){
            dollar = 1;
        }
      }

    if(capital == 1){
        printf("\nCapital test:\tPassed\n");
    }else{
        printf("\nCapital test:\tFailed\n");
        }
        
    if(number == 1) {
        printf("Number test:\tPassed\n");
    }else{
        printf("Number test:\tFailed\n");
        }
        
    if(dollar == 1){
        printf("Dollar test:\tPassed\n");
    }else{
        printf("Dollar test:\tFailed\n");
        }
        
    if (capital == 0 || number == 0 || dollar == 0){
        printf("\nYour password needs improving.\n");
    }else{
        printf("\nYour passowrd is strong.\n");
    }
   return 0;
}


I think this is pretty good, but let me know if you think it can be improved.
Cheers
Matt

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Minh Quan Vu · November 20, 2014
/images/forum/upload/2014-11-20/b1bfeee829c4c59735f80bd7a5c1414a.png

as you can see, even the input is lower case, it still say Capital test: passed
0 Tyler Borntraeger · November 26, 2014
hey guys...I did it...

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

int main() {

//Declarations
int i, cap = 0, low = 0, dig = 0, sym = 0;
char password[12];

//Input
printf("Enter a password with at least one capital letter, at least one number, and at least one symbol \n");
scanf(" %s", password);

//Conditionals
for (i = 0; i < strlen(password); i++) {
if (isupper(password) )
cap++;
else if (islower(password) )
low++;
else if (isdigit(password) )
dig++;
else
sym++;
}

//Output checkers
if ( (cap >= 1) && (dig >= 1) && (sym >= 1) )
printf("Your password is sufficient!");
else
printf("Try again");
printf("\nDigits: %d\nUppercase: %d\nSymbol: %d", dig, cap, sym);
return 0;
}
0 Minh Quan Vu · November 21, 2014
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

main ()
{

    char password[20];
    int check = 0;
    int capital = 0, number = 0, dollar = 0;

    printf("This program will check the strength of your password.\nPlease enter your password (this is totally not a scam): \n ");
    scanf("%s", &password);

    for(check = 0 ;check != 0 ; check++){
        if( isupper(password[check]) ){
            capital++;
            }
        if( isdigit(password[check]) ){
            number++;
        }
        if( password[check] == '$' ){
            dollar++;
        }
      }

    if(capital >= 1){
        printf("\nCapital test:\tPassed\n");
    }else{
        printf("\nCapital test:\tFailed\n");
        }

    if(number >= 1) {
        printf("Number test:\tPassed\n");
    }else{
        printf("Number test:\tFailed\n");
        }

    if(dollar >= 1){
        printf("Dollar test:\tPassed\n");
    }else{
        printf("Dollar test:\tFailed\n");
        }

    if (capital == 0 || number == 0 || dollar == 0){
        printf("\nYour password needs improving.\n");
    }else{
        printf("\nYour passowrd is strong.\n");
    }
   return 0;
}


I just fixed some small details. Hope this will work fine
0 Mayur Joshi · November 21, 2014
minh you typed for (check=0 ;check !=0 ; check++) , that will get stuck in infinite loop for sure.
you should type for (check=0 ;check!='/0' ; check++) , the '/o' is a null character present at end of a string.
0 Mayur Joshi · November 21, 2014
wonder time \0 -> my mistake
0 c student · December 7, 2014
/images/forum/upload/2014-12-07/a5951bf183619ccfa5bffd3c249d91bc.png
0 Tyler Borntraeger · November 23, 2014
Hey guys, tried the challenge and went through numerous changes - many of you helped with modifications...So this below works, except for when you enter a capital and a digit but no symbol - it still works somehow (a symbol paired with a capital doesn't work, only a capital doesn't work, and a lowercase doesn't work, as I wanted).  I've looked over the code, but nothing seems to be glaringly obvious.  Help me out?

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

int main() {

//Declarations
int counter, cap = 0, dig = 0, sym = 0;
char password[20];

//Input
printf("Enter a password with at least one capital letter, at least one number, and at least one symbol \n");
scanf(" %s", password);

//Conditionals
for (counter = 0; counter <= 20; counter++) {
if (isalpha(password[counter]) ) {
if (isupper(password[counter]) ) {
 cap++;
}
}
if (isdigit(password[counter]) ) {
dig++;
}
else {
sym++;
}
}

//Output checkers
if (cap >= 1 && dig >= 1 && sym >= 1) {
printf("Your password is sufficient!");
}
else {
printf("Try again \n");
}
return 0;
}
0 Tyler Borntraeger · November 23, 2014
This is my for loop that I've modified to fit your correction to Mayur Joshi's correction to Minh's code:

for (counter = 0; counter != \0; counter++) {

but the compiler still gives me an error.  I know exactly where the error is, obviously it's the loop terminator in the middle, but according to what I see on the forum, that is how I'm apparently supposed to syntax that loop for determining a string terminator in a loop.  Here's the context of my modified code just in case:

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

int main() {

//Declarations
int counter, cap = 0, dig = 0, sym = 0;
char password[12];

//Input
printf("Enter a password with at least one capital letter, at least one number, and at least one symbol \n");
scanf("%s", password);

//Conditionals
for (counter = 0; counter != \0; counter++) {
if (isupper(password[counter]) ) {
cap++;
}
if (isdigit(password[counter]) ) {
dig++;
}
else {
sym++;
}
}

//Output checkers
if (cap >= 1 && dig >= 1 && sym >= 1) {
printf("Your password is sufficient!");
}
else {
printf("Try again");
}
return 0;
}

Still not working, and I also noticed that the uppercase test is actually completely useless.  Additionally, I enter a 4 and it gives me Your password is sufficient.  So my digit code should be correct, but I just don't know what I'm doing wrong.  I really wanna get this perfect.
0 Tyler Borntraeger · November 23, 2014
ohhhh do you have to do the single quotes with it as well?  Sorry if you put that little character in your videos I haven't gotten to it yet - and I'm not reading any "books" I'm just gathering what I can from your videos and from these guys ^^^
0 Minh Quan Vu · November 19, 2014
Personally, you should add "if( isalpha(password[check]) ) " because if you don't, even the input is lower case letters, it still count as upper case letters
  • 1
  • 2

C

107,270 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator