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 2
Oldest  Newest  Rating
0 Tyler Borntraeger · November 24, 2014
alright I'll think about it but do you put the "\0" in single quotes in the for loop?
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 c student · December 7, 2014
/images/forum/upload/2014-12-07/a5951bf183619ccfa5bffd3c249d91bc.png
-1 subham goyal · December 10, 2014
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{
  int password = 'abcd143';

  if(isalpha(password) && isdigit(password)){
    printf("% c is a password", password);
  }else
  {
       printf("% c is not a password", password);
  }

  return 0;
}
  • 1
  • 2

C

107,081 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator