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 Matt Thomson · November 19, 2014
I'd have to agree with Wonder Time, I don't see the need for the extra if statement.

Minh, Could you give an example of when this program would break down without the isalpha function?
0 Mayur Joshi · November 19, 2014
for check loop, you are scanning upto end of array, possibly it will take garbage values after '/0' as integer so number variable is affected. you can take for (check=0 ;check!='/0' ; check++)
also if you change conditions from capitl==1 to capitl>=1 that might help to improvise this code.
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?
-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,040 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator