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
I read this:

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

 but I don't know the solution for my problem?
+1 c student · December 8, 2014
first off, let me clarify that seg fault means that there is an attempt to access (read/write/whatever) prohibited data which lies outside of your program's memory stack, that is, basically outside of what your program has access to.  this is a security measure to protect damage to other program's data.

the reason why you are getting the seg fault is that although you have placed the check for the length of your password, you did not do anything other than print a message!  your program continued to run despite having all of your variables being flooded everywhere.  i believe your attempt to check your variables triggered the seg fault because the values have spilled into other memory stacks outside your program's reach,  a simple fix for this is to have your program terminate after printing the message with a function such as exit (int status) which is found in the stdlib.h header file.

i completely agree with haskell student.  sometimes you do not need to store things in memory but instead can check things on the fly at runtime.  getchar() is an excellent function for this type of situation and i highly recommend it because it minimizes any potential nasties buffers may cause.

you may need to check out www.asciitable.com for this since you'll need to familiarize yourself with different, awesome characters besides the standard alphabet/numbers/special characters!

good luck! 8-)
  • 1
  • 2

C

124,754 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator