C challenge1

0 Supun Rangana · February 8, 2016
why this isnt working..??

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

int main()
{
    char pswrd[9];
    int u=0; // uppercase
    int d=0;//digit
    int s=0;//$ sign
    int X=0;//loop count

    printf("Enter your password.? (8)");
    scanf(" %s",&pswrd);

    printf("wait\n");

    for(X=0;X=7;X+=1){
        if(isupper(pswrd[X])){
            u++;

        }else if (isdigit(pswrd[X])){
            d++;

        }else if ((pswrd[X]='$')){
            s++;
        }
        X++;

    }




            if ((u>=1)&&(d>=1)&&(s>=1)){
            printf("strong password");
            }else {
            printf("weak password");
}
            return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Perverse Programmer · February 22, 2016
We have another programmer who thinks "I know how to write code that works for my C implementation, so I can teach C"... How perverse! I LOVE IT!
0 Supun Rangana · February 12, 2016
thanx again....I'll look forward to that...
0 kuldeep kanzariya · February 16, 2016
instead of          >>>>>else if ((pswrd[X]=='$'))<<<<<<<<<<<<<<

U should use   >>>>>>>>>>>>  ispunct(pswrd[X])
0 Linguist Llama · February 8, 2016
There are multiple issues I would like to work through with you, but I want to cover one thing at a time so I can be sure that you understand.

for(X=0;X=7;X+=1){

What does x=7 mean? The = operator only means one thing in C...

}else if ((pswrd[X]='$')){

Similarly, what does pswrd[x]='$' mean?

Please respond to confirm that you've understood so we can move on to the next issue.
0 Supun Rangana · February 8, 2016
Thank you very much for the reply...i have written this for a 8 character password..X is for the place in the pswrd array..
0 Supun Rangana · February 8, 2016
@Linguist Llama thanx for opening my eyes...i figured out some problems..now its working...can you please tell me if there is anything wrong..hope your assistant in future..

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

int main()
{
    char pswrd[8];
    int u=0; // upper case letter
    int d=0; // number
    int s=0; // $ sign
    int X=0; // loop count

    printf("Enter your password.? (8)");
    scanf(" %s",pswrd);

    for(X=0;X<=8;X++){
        if(isupper(pswrd[X])){
            u=1;

        }else if (isdigit(pswrd[X])){
            d=1;

        }else if ((pswrd[X]=='$')){
            s=1;
        }


    }




            if ((u==1)&&(d==1)&&(s==1)){
            printf("strong password");
            }else {
            printf("weak password");
}
            return 0;
}
0 Linguist Llama · February 9, 2016
Yes, there are still problems here. I note you partially fixed your scanf. That's good. We'll delve deeper into that later...

char pswrd[8];

pswrd is an array of 8 chars, we can count them: pswrd[0], pswrd[1], pswrd[2], pswrd[3]... There's 4 so far... pswrd[4], pswrd[5], pswrd[6], pswrd[7]... and there's 8. Notice how the last one is pswrd[7]?

Now if we take a look at your loop

for(X=0;X<=8;X++){
if(isupper(pswrd[X])){

We can see that your loop evaluates pswrd[8], which would be the 9th char in the array, if the array had 9 chars.

Also, isupper and isdigit expect an unsigned char (that is, only positive values) or EOF, and bad things can happen when you violate that expectation: http://stackoverflow.com/questions/8877410/visual-c-debug-assertion-failed

I recommend using isupper((unsigned char) pswrd[X]) (and similar for isdigit) to convert the (possibly negative) char value to a (definitely positive) unsigned char value.
  • 1

C

107,049 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator