Bucky's Challenge #1 - doesn't like spaces

+1 Erkut Doru · October 14, 2016
Okay, so I'm totally new to coding and I've only learnt the code in the videos up until this challenge.
I see other's have used code that's not been shown yet.
And most I've seen don't have the $ sign.
So I got the code to work when there are no spaces, but it doesn't, does anyone know how I can fix this?

//  Check Password contains Upper, Number and $ sign


#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()

{
    char password[20], dollar;
    int i, upper, numb;

    printf("Please type your password: ");
    scanf("%s", password);


    for (i = 0; i <= 21; i++)

        {
            if(isupper(password))
                { upper = 1;}
            if(isalpha(password))
                {numb = 1;}
            if(password == '$')
                {dollar = 1;}
        }

    if ((upper == 1) && (numb == 1) && (dollar == 1))

    {
        printf("Sweeeeeeeeet! You're in!\n");
    }

    else

    {
        printf("Nope, Try Again.\n");
    }

    return 0;

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Erkut Doru · October 22, 2016
Doh! Figured it myself. 
scanf()

no spaces.:P
0 Kent Vugs Nielsen · October 22, 2016
Hello Erkut Doru.

To start of with, you put your variable "password" to be 20 bytes, but in your for loop you set the iteration to be 21. After what I know then it's "good practice", setting it to the same value as your variable.
Secondly, inside of your for loop you check whether there is an upper in your password and whether there is a letter (isalpha) where I would think you would check on whether it is a number, which is "isdigit".
You wondered about that some of the other solutions which does'nt check after the dollar sign, I do not know about other people's code, but in my solution there will be checked on whether there are any special characters and not just the dollar sign.

I do not quite understand your last question about "the spaces", but usually in a password there may not be "spaces" in it.

My Solution: https://thenewboston.com/forum/topic.php?id=12913

Theres is only 2 things i have used in my solution that i guess you havent learned yet and that's functions just like main is a function, but there is still a difference between a "normal" function and the main function.
The second thing i've used is boolean's and that just instead of 1 and 0 it sets either true or false. So i used them instead.


Hoped it helped!
best regards
Kent
0 Muhammad Zunaid Karim · December 22, 2016
I didn't do the dollar thing as I thought it wasn't very practical... this works for me..

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main()
{
    char password[20];
    int i, upper=0, lower=0, digit=0, minLength=8;
    printf("\n Please type a password with 8 characters \n with at least 1 capital letter, 1 number. \n");
    printf("New password: ");
    scanf(" %s", password);
    if(strlen(password)>=minLength){
        for (i=0; i<20; i++){
            if(isupper(password)){
               //printf("\n %c", password);
               upper++;
              // printf("\n %d", upper);
            }
            else if(islower(password)){
                //printf("\n %c", password);
                lower++;
                //printf("\n %d", lower);
            }
            else if(isdigit(password)){
                //printf("\n %c", password);
                digit++;
                //printf("\n %d", digit);
            }
            /*else if(upper==0||lower==0||digit==0){
                printf("\nYour password doesn't fulfil the requirements");
                break;
            }*/
            else if(upper==0){
                printf("\nYour password doesn't have any CAPITAL letter");
                break;
            }
            else if(digit==0){
                printf("\nYour password doesn't have any NUMBERS");
                break;
            }
            else if(upper>0 && lower>0 && digit>0){
                printf("\nYour password is strong!");
                break;
            }
            else{
                printf("\n please try again with at least\n 1 capital letter, 1 number\n");
                break;
            }
        }
    }else {
        printf("\nYour password requires atleast 8 characters");
    }
    printf("\nProgram is quitting...");
    return 0;
}
0 King Arthas · April 23, 2017
here is mine if someone can give me a feedback , thanks.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>


int main()
{
    char password[20];
    int i, a, b ,c , d;

    a = b = c = 0;

    printf("Enter your password you can use an upper case letter, a digit and the dollar symbol): ");
    scanf(" %s", &password);

    for( i= 0; i <= 20 ; i++){
        if ( isupper(password)){

            a++;


                }else if (isdigit(password)){
                b++;


                    }else if ( password == '$'){
                    c++;

        }

    }


    if ( a+b+c >= 3){
        printf("The choice of the password is good \n");
    }else{

        printf("Please type another password later \n");
    }
    return 0;
}
0 Criston Mascraenhas · April 23, 2017
put        scanf("%s",&password);       in 11 line
  • 1

C

131,119 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator