Problem with code ( C )

0 votes
asked Jan 14, 2019 by anonymous
So the task is to write a piece of code that checks a password for an uppercase letter, lowercase letter, number and a sign, then print out if it's a strong password or a weak one.
However im running into a problem with my code, i've recently started coding so im not very experienced, if anyone could help me fix the code that would be great.

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100

int main()
{

    char password [MAX];
    int up=0,low=0,sign=0,num=0,len=0;

    scanf("%s", &password);

    for(int i=0; i<=password[MAX]; i++)
    {
        if(isalpha(password[i]))
        {
            if(isupper(password[i]))
                up++;
            else
                low++;
        }
        else
        {
            if(isdigit(password[i]))
                num++;
            else
                sign++;
        }
    }

    if(up && low && sign && num)
        printf("Strong Password\n");
    else
        printf("Weak password\n");

        printf("%d %d %d %d", up, low, sign, num);

    return 0;
}

3 Answers

0 votes
answered Jan 15, 2019 by anonymous

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100

int main()
{

    char password [MAX];
    int up=0,low=0,sign=0,num=0,len=0;

    scanf("%s", &password);

    for(int i=0; i<=password[MAX]; i++)
    {
        if(isalpha(password[i]))
        {
            if(isupper(password[i]))
                up++;
            else
                low++;
        }
        else
        {
            if(isdigit(password[i]))
                num++;
            else
                sign++;
        }
    }

    if(up && low && sign && num)
        printf("Strong Password\n");
    else
        printf("Weak password\n");

        printf("%d %d %d %d", up, low, sign, num);

    return 0;
}

+1 vote
answered Jan 17, 2019 by Jyothi_Rk

Hi,

Below is the solution for your problem.Just make a  small change in 'for' condition,your code will work fine :-)

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100

int main()
{

    char password [MAX];
    int up=0,low=0,sign=0,num=0,len=0;

    scanf("%s", &password);

    for(int i=0;password[i] != NULL; i++)
    {
        if(isalpha(password[i]))
        {
            if(isupper(password[i]))
                up++;
            else
                low++;
        }
        else
        {
            if(isdigit(password[i]))
                num++;
            else
                sign++;
        }
    }

    if(up && low && sign && num)
        printf("Strong Password\n");
    else
        printf("Weak password\n");

        printf("%d %d %d %d", up, low, sign, num);

    return 0;
}

commented Jan 17, 2019 by Damjan Apostoloski (110 points)
Thank you, that fixed the problem.
0 votes
answered Jan 17, 2019 by 157y1a05a5 1 flag
#include<stdio.h>

#include<string.h>

main()

{

 char str[]="dismantle my enemies and update";

char str1[11];

int i,len;

len=strlen(str);

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

  {

     str[i]=str[i]^10;

    printf("%c",str1[i]);

   }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...