Challenge #1

+3 Debora Karalić · January 19, 2015
Hi, this is how I did this challenge.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
    int upperCase=0, number=0, br=0, lowerCase=0;
    char password[50];

    printf("Enter your password <longer than 8, uppercase, lowercase and a number>\n");

     printf("Type your password");  
scanf("%s", password);
    while (strlen(password)<=8);

    for (br=0; br<=strlen(password); br++)
        if (isupper(password))
    for (br=0; br<=strlen(password); br++)
        if (isdigit(password))
    for (br=0; br<=strlen(password); br++)

    if (number && lowerCase && upperCase)
        printf("\nPassword contains %d uppercase, %d numbers, %d lowercase and its ok.", upperCase, number, lowerCase);
    else printf("Think before you write.");
    if (strlen(password)-upperCase-number-lowerCase!=0)
        printf("\nYou have %d other char in your password.", strlen(password)-upperCase-number-lowerCase);
    return 0;

Post a Reply


Oldest  Newest  Rating
0 Aleksander Olewinski · January 19, 2015
There is a never ending loop. If someone type less than 8 characters loop will never end. 

scanf("%s", password);
while (strlen(password)<=8);
0 c student · January 19, 2015
i'm guessing that's the point.
0 Aleksander Olewinski · January 20, 2015
I understand that in your opinion code is good and complete when there are never ending loops? You think Deborah wanted to make it this way. Soon I will post my next version of challenge 1 with fgets and pointers :).

Cheers mate.
+1 H. P. Lovecraft · January 21, 2015
Hi, this code does not function correctly.

There are two major mistakes in this code. 

  1. isupper(), islower(), isdigit() does not work on an entire array of chars, only one char at a time.

  2. if (number +lowerCase + upperCase>2) is not going to work correctly. If the password only contains lower case letters, and the length of the password is 8 chars, what is the value of lowerCase going to be? It is going to be 8, and 8 > 2 is true. 

How can we solve this?
Firstly check every character by doing isupper(password
), isdigit(password
), islower(password
). That way you check every single position of the string.

For the second problem, just replace "if (number +lowerCase + upperCase>2)" with "if (number && lowerCase && upperCase)". In C all values except for 0 are true. This means that it will evaluate to true if there is at least one lower case, upper case and digit, which is the goal of the task.

If you do this, the code will work correctly. 

Optional tips for improvement:
This current solution is not very efficient. Instead of calling strlen(password) multiple times, try to see if you can only call it once and store it in a variable that you reuse for the rest of the program. Calling it multiple times is not necessary, unless the length  has changed.

Secondly you have 3 for loops, is this entirely necessary? Try removing two of your for loops, and have tree if statements inside one for loop instead. This way you don't have to check every single character of the array 3 times. Imagine if you had to check a password that was 100 characters long. 

With you current code, you would check every single character 9 times in total. Every for loop is going through 100 characters + you are calling strlen many times (which each time goes through 100 characters). In total, you would have checked 900 characters, when you could have gotten away with 200 (1 strlen call + 1 for loop).

I am not saying you need to be constantly aware on how to achieve the best solution, but once you have a solution try to think how you could redundant operations. Best of luck on your programming journey.
0 Debora Karalić · January 25, 2015
Thanx for commenting on my code,
I didn't wanted to make never-ending loop, it's just preventing user from entering password shorter than 8 characters. And about the if(numer+lowerCase+upperCase>2), it was just a mistake with uploading the wrong code. I had problems with running my code when using one for loop, so instead I used 3 (guess something is wrong with my Visual studio).
Thank you for trying to help me, cheers to you all :)
0 Emir ARITURK · January 28, 2015


#define LENGTH 20

  void passwordCheck (char password[LENGTH]){
       int i ; //loop counter
        int upperCase = 0 ; // number of Upper case(s)
          int lowerCase = 0;// number of Lower case(s)
          int digit = 0 ; //number of digit(s)
             int dollar = 0 ; //number of dollar sign(s)
         for ( i = 0; i <= strlen(password) ; i++){
            if( isupper( password ) )
             upperCase++ ;
               if( islower( password ) )
                 lowerCase++ ;
                    if( isdigit( password ) )
                   digit++ ;
                    if( password == '$' )
                        dollar++ ;
          if (upperCase >= 1 && lowerCase >= 1 && digit >= 1 && dollar >=1)
          printf("Password is safe!!\n\n");
          printf("Password is not safe enough..");      
int main(){
   char passworddd[LENGTH];
        printf("\n\nPlease enter the password that you want to create (at least 5 characters) : ");
         scanf("%s\n\n", passworddd[LENGTH]);
       passwordCheck( passworddd[LENGTH] );
    return 0;

Hey friends, hope you all are doing well! I was going through that challenge thing and I've ridicilously run into an error! I really can't see what's wrong with the code above. Long story short, could anyone just help me through that? Thnx!
0 Emir ARITURK · January 28, 2015
Of course I know how to read errors,but the problem is, the compiler stops working when I execute it for this code.
0 Emir ARITURK · January 28, 2015
Thanks a lot c student, you made think deeper about it than I've found the solution in a better way, thnx!! ;)
0 student abdo · January 28, 2015
plz help me i don't know what i can do in these program 
#include <iostream>
#include <stdio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
int i;
char *t[4];
char m;
for (i=0;i<=3;i++){
printf("donnez un jour%d?\n",i+1);
for (i=0;i<=3;i++){printf("jour %d:%s\n",i+1,t);

return 0;
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator