Challenge #1

+1 Aaron McAnally · March 26, 2015
So I'm doing Challenge one and I have my basic idea down. I pass the character array "password" another variable number in a for loop to check every individual item for upper case, but I'm having issues with the symbols. In this particular case I need a way to set "if(password[letter] != anynumber && anyinteger){oChar++ continue;}" but I can't really find anything on google, so I figured some more experienced people may have more effective advice.






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

int main()
{
    char password[15];
    int letter = 0;
    int uLetter = 0;
    int oChar = 0;
    int uChar = 0;

    printf("Please enter a password. ");
    scanf(" %s", password);

    for(letter=0; letter<=15; letter++)
        {
            if(isalpha(password[letter]))
                {
                    if(isupper(password[letter]))
                        {
                            uLetter++;
                            continue;
                        }
                        else if(islower(password[letter]))
                            {
                                continue;
                            }

                }
            else if(isdigit(password[letter]))
                {
                    continue;
                }
            else
                {

                }

        }
    if( uLetter && oChar > 0 )
        {
            printf("Your password is satisfactory.");
        }
    else
        {
            printf("Looks like you need to try again broski");

        }


    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Jacky Leung · March 29, 2015
#include <stdio.h>

int main()
{
    char pw[20];
    int uppercase = 0;
    int number = 0;
    int dollarsign = 0;
    int i;

    printf("Enter your password: \n");
    scanf(" %s", pw);

    for (i = 0; i < 20; i++){
        if (isupper(pw))
            uppercase = 1;
        if (isdigit(pw))
            number = 1;
        if (pw == '$')
            dollarsign = 1;
    }
    if (uppercase == number == dollarsign == 1)
        printf("Strong password!");
    else printf("Weak password!");

    return 0;
}
0 Aaron McAnally · March 28, 2015
Well with questions 4 I wanted to think about it on my own before I said something that would come across as ignorant, and I see what you're saying. "isalpha" is unnecessary, because if it fulfills the "isupper" then it's fairly obvious that it would belong to the alphabet. I guess what I don't understand is the unsigned character or EOF value. I thought I had watched Bucky's tutorials pretty thoroughly but I don't remember any mention of them. Probably due to my less than stellar memory.
0 Aaron McAnally · March 28, 2015
Well I've been working on it a bit, and I'm still having some issues, but I've refined it slightly:

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

int main()
{
    char password[15];
    int letter = 0;
    int uLetter = 0;
    int oChar = 0;

    printf("Please enter a password. ");
    scanf(" %s", password);

    for(letter = 0; letter <= 14; letter++)
        {
            putchar(password[letter]);

            if(isalpha(password[letter]))

                {
                    if(isupper(password[letter]))
                        {
                            uLetter++;
                        }
                }

            else if(ispunct(password[letter]))
                {
                    oChar++;
                }
        }
            putchar('\n');

        if(oChar && uLetter > 0)
            {
                printf("Your password is satisfactory.");
            }
        else if( oChar || uLetter <= 0 )
            {
                printf("Try again.");
            }

    return 0;
}


Now the issue is that when I define the variable password it's storing trash characters which gives me a false positive.
(Oh and Arjun Patel, I tried working on compacting my if statements, but for organizational purposes I like how I have them at the moment. It lets me know instantly what's going on, I think I'll keep it this way until I gain some more experience)

1.) Well I wanted to be able to store up to 14 characters, but as I mentioned before that it's filling up the excess memory
2-3.) I've determined (of course) that the loop runs the entire 15 times no matter how many characters are entered into the variable.
0 Dol Lod · March 26, 2015
No problem. Good luck!
0 Aaron McAnally · March 26, 2015
Oh I see, thank you very much for your candid opinion. I'll be sure to review my use of if functions and solidify my knowledge.

I guess I was just too focused on trying to account for everything I put unnecessary work into it and made it into a bigger monster than it needed to be.

I'll rewrite my code keeping your advice in mind and hopefully I'll make something more efficient, and more importantly, something that actually works as intended.
0 Dol Lod · March 26, 2015
This will actually go out of bounds <=15 when the array size is 15 means you have undefined behaviour. Additionally, ochar is never incremented so it will never be accurate. 
Other points:

2 nested if conditions can be combined with an && statement.
If(x==2)
{
    if(y==4)
         y++
}

This could be rewritten as if(x==2 && y==4)y++;

More importantly, you don't need that many continues. By default you go to the next iteration. There is no need to account for every case with continue or using empty braces. 

I strongly recommend rewriting this code and making it simpler. Watch more tutorials on if statements and how to use continue and learn if those are really necessary. 

ispunct is a function that can check for symbols that is part of the library you are already using in ctype.h.
  • 1

C

107,330 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator