Challenge 1 ( the beginners way)

+2 Shahriar Khan · February 8, 2015
#include <stdio.h>
#include <stdlib.h>


int main()
{
    char password[5];
    char symbol = '$';

    printf("Enter your password with at least one Uppercase, one Lowercase, one Digit & one $ sign\n ");
    printf("(maximum six characters) Type your password : ");

    scanf(" %s", password);

    if ( ( isupper(password[0]) || isupper(password[1]) || isupper(password[2]) || isupper(password[3]) || isupper(password[4]) || isupper(password[5]) ) && ( islower(password[0]) || islower(password[1]) || islower(password[2]) || islower(password[3]) || islower(password[4]) || islower(password[5]) ) && ( isdigit(password[0]) || isdigit(password[1]) || isdigit(password[2]) || isdigit(password[3]) || isdigit(password[4]) || isdigit(password[5]) ) && ( password[0]==symbol || password[1]==symbol || password[2]==symbol || password[3]==symbol || password[4]==symbol || password[5]==symbol ) ){

    printf("valid password. thank you!");
}else{
    printf("invalid password. try again.");

}
return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · February 8, 2015
and what if the spec was changed to a length greater than 4?
one of the main things a computer is fantastic at is repetition and the ability to be scalable, all aspects which are not present in this, however, since you are a beginner, it's fine... for now.  even i remember doing something as incredibly horrifying like this.

things to note:
your input only takes strings up to 4 characters (4 to store the string and the last element to store the string terminator),
notice patterns in your code.  anything with patterns can be set into combinations of loops and conditional statements. for example, a simple program to sum up the simple pattern: 1+2+3+..+10:
int i, sum = 0;

for (i = 1; i < 11; i++) {
   sum += i;    // 0+1, 1+2, 3+3, 6+4, ..., 45+10 = 55
}
0 Mathias Frits Rørvik · February 8, 2015
Hi, this is certainly one way to solve it, but not the best. 

It works, but like c student said, it can't handle passwords longer than 4 characters, but you made one mistake. You are checking at position 5, remember with arrays we start counting at 0. So for an array of 5 chars, you have position 0, 1, 2, 3, 4. You are checking at a 5th position, this can cause your program to randomly crash and do weird stuff.
0 J show · February 8, 2015
I actually just learned that it starts at zero last week. ha and we just learned the while and for loop as well. I'm also taking matlab this semester so hopefully the two dont screw me up to much haha
0 Shahriar Khan · February 9, 2015
Mathias Frits Rørvik . you are right. the character string named password should be [6]. :)
  • 1

C

107,309 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator