Shravan Niranjan
Shravan Niranjan

Reputation: 29

Input a password from the user and check if it has a char, alphabet and digit

My doubt is that, the program works fine if I enter a password that fills the array. However if I enter a password such as "apple25", I think it counts the blank spaces in the array as characters and even declares "apple25" as a valid password.

How do I prevent this from happening? Does it count the string terminator also as a char? Because even "AAPPLLEE2" comes as a valid password.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0


int main()
{
    char arr[10];
    printf("Enter a password. \n");
    scanf("%s",arr );
   // printf(arr);
    char ch;
    int i;
    int alp=0, dig=0, c=0;


    for (i=0; i<10; i++)
    {
       ch=arr[i];

       if(isalpha(ch))
       {
           alp++;
          // printf(" alpha is %d \n",alp);
       }
       else if(isdigit(ch))
       {
           dig++;
          // printf("digit is %d \n",dig);
       }
       else
       {
           c++;
         //  printf("charectores are %d \n",c);
       }

    }
    if(alp>=1&&dig>=1&&c>=1)
        {
            printf("Valid Password");
        }
    else{
            printf("Invalid Password");
        }
        return 0;
}

Upvotes: 1

Views: 1285

Answers (4)

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

  • Point 1. Always initialize your automatic local variables. Saves you from accidentally accessing un-initialized value.

    char arr[10] = {0};
    
  • Point 2. Always limit your input length. Saves you from running into undefined behavior by overrunning the allocated memory by longer-than-expected input.

    scanf("%9s",arr );
    
  • Point 3. Use strlen() to loop over only the valid part of input.

Upvotes: 3

DarkDust
DarkDust

Reputation: 92306

There are several issues here:

  • Try entering a password with a size of 10 or longer. You'd overflow arr.
  • You don't stop at the terminating NULL character (value 0). In fact, you count the terminating NULL as "character" and increase c when you come across it.
  • Any garbage afterward is also processed.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

I think it counts the blank spaces in the array as charecters and even declares "apple25" as a valid password.

Your assumption is almost right. Your program does count characters after the end of user input, but they are not necessarily blanks.

You get this effect with "AAPPLLEE2" because your code examines ten characters of a nine-character string. The tenth character is null terminator, which is neither a digit nor a letter.

You need to use the actual length for the limit of the loop, rather than hard-coding the limit of ten.

You should also fix the potential buffer overrun:

char arr[11];       // <<== Add one more character for '\0'
printf("Enter a password. \n");
scanf("%10s",arr ); // <<== Tell scanf how big is your buffer

Upvotes: 2

P.P
P.P

Reputation: 121347

That's because your loop runs over the entire, not just upto the characters you read.

Instead do,

    size_t len = strlen(arr);

    for (i=0; i<len; i++) {
      ...
    }

You may also want to use fgets() to avoid buffer overrun. If you enter more than 10 characters, you'll overflow arr, which is undefined behaviour.

Instead of

scanf("%s",arr );

do

fgets(arr, sizeof arr, stdin);
char *p = strchr(arr, '\n');
if(p) *p = 0; //trim the trailing newline, if present

Upvotes: 2

Related Questions