C Programming Tutorial - 33 -Challenge #1!

+3 Dejan B · November 30, 2014
Hello to everyone!
I try and I think it works fine, but can you tell me some improvement?
Thanks!
#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.\nEnter a password with:\nat least 8 characters long,\nat least one capital letter,\nat least one number\nat least one $ sign:\n");
    scanf(" %s", &password);

    for(check=0; check < strlen(password); check++){

        if( isalpha(password[check]) ){
            if( isupper(password[check]) ){
            capital++;
            }
        }

        if( isdigit(password[check]) ){
            number++;
        }

        if( password[check] == '$' ){
            dollar++;
        }
      }

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

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

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

    if(strlen(password) >= 8){
        printf("Minimum required length: %d\tPassed\n", strlen(password));
    }else{
        printf("Minimum required length: %d\tFailed\n", strlen(password));
        }

    if (capital == 0 || number == 0 || dollar == 0 || strlen(password) <=7){
        printf("\nYour password needs improving.\n");
    }else{
        printf("\nYour password is strong and have %d characters!\n", strlen(password));
    }
   return 0;
}




Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 Dejan B · December 7, 2014

#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.\nEnter a password with:\nat least 8 characters long,\nat least one capital letter,\nat least one number\nat least one $ sign:\n");
    scanf(" %s", password);

    if( strlen(password)>21 ){
        printf("Enter maximum 20 characters! \n");
        }else{

        for(check=0; check < (int)strlen(password); check++){

        if( isalpha(password[check]) ){
            if( isupper(password[check]) ){
            capital++;
            }
        }

        if( isdigit(password[check]) ){
            number++;
        }

        if( password[check] == '$' ){
            dollar++;
        }
      }

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

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

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

    if(strlen(password) >= 8){
        printf("Minimum required length: %d\tPassed\n", (int)strlen(password));
    }else{
        printf("Minimum required length: %d\tFailed\n", (int)strlen(password));
        }

    if (capital == 0 || number == 0 || dollar == 0 || strlen(password) <=7){
        printf("\nYour password needs improving.\n");
    }else{
        printf("\nYour password is strong and have %d characters!\n", (int)strlen(password));
    }
        }
   return 0;
}



Now  overflowing my password buffer "only" crashes the program in codeblocks and in coding ground I have this:

/images/forum/upload/2014-12-07/5ff9f425c00498fcf6d7a5389d041376.jpg
0 Dejan B · December 7, 2014
I read this:

http://en.wikipedia.org/wiki/Segmentation_fault

 but I don't know the solution for my problem?
  • 1
  • 2

C

128,201 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator