student
student

Reputation: 15

I can't figure out how to use the strlen in my C code

I just started learning C. I am supposed to create a password program where the user inputs strings that are at least 8 characters longs. It also needs to have one upper case, one lower case, one digits, and one symbol from this set {#, %, +}. I can't try to figure out the loop where it prints invalid when the user doesn't type in at least 8 characters. I tried using strlen but it gives me an error passing argument 1 of ‘strlen’ makes pointer from integer without a cast can someone help me?

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

int main()
{
  int n;
  int k;

  char ch;
  unsigned char uFlag = 0, cFlag = 0, dFlag = 0, hFlag = 0, aFlag = 0;
  printf("Enter a password: ");

  ch = getchar();

  n = strlen(ch);
  k = n - 1;

  while (ch != '\n')
  {

    if(ch >= 'A' && ch <= 'Z')
      uFlag = 1;

    else if(ch >= 'a' && ch <= 'z')
      cFlag = 1;

    else if(ch >= '0' && ch <= '9')
      dFlag = 1;

    else if(ch == '#' || ch == '%' || ch == '+')
      hFlag = 1;

    else if (k >= 8)
      aFlag = 1;

    ch = getchar();
  }

  if (uFlag == 1 && cFlag == 1 && dFlag == 1 && hFlag == 1 && aFlag == 1)
  {
    printf("Password entered is valid.\n");
  }
  else
  {
    printf("Password entered is invalid.\n");
  }


  return 0;
}

Upvotes: 0

Views: 193

Answers (3)

user3629249
user3629249

Reputation: 16540

regarding:

ch = getchar();

n = strlen(ch);

the function getchar() only inputs a single char, not a string.

the function strlen() expects a NUL terminated array of characters.

Suggest inputting the password using;

fgets( buffer, sizeof( buffer ), stdin );

rather than using many calls to getchar()

then use:

buffer[ strcspn( buffer, "\n" ) ] = '\0';

to remove an newline character

then looping through the buffer[], checking each character

for( size_t i=0; buffer[i]; i++ )
{
    // perform all the needed checks of the password
}

Upvotes: 0

Abhishek Bhagate
Abhishek Bhagate

Reputation: 5766

You must be used an array of characters and not an char. In your code, you have written -

n = strlen(ch);

However, strlen() accepts char* or an array of character or pointer to a string as parameter. You are passing ch which is of type char and is the wrong parameter data type. This is main reason why you are getting the error.

I have made minor edits in your program which you can refer below -

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

int main()
{
  int i=0; //i to maintain current index in char array-ch

  char ch[100],c; //or you could use dynamic allocation by asking user for input, c will read user input
  unsigned char uFlag = 0, cFlag = 0, dFlag = 0, hFlag = 0, aFlag = 0;
  printf("Enter a password: ");

  c = getchar();   //read the input into character as you did
  ch[i]=c;         //assign the character to current index in ch array

  while (ch[i] != '\n')   //check until user hits enter
  {

    if(ch[i] >= 'A' && ch[i] <= 'Z')
      uFlag = 1;

    else if(ch[i] >= 'a' && ch[i] <= 'z')
      cFlag = 1;

    else if(ch[i] >= '0' && ch[i] <= '9')
      dFlag = 1;

    else if(ch[i] == '#' || ch[i] == '%' || ch[i] == '+')
      hFlag = 1;

//the below should be if and not if else as in your posted code for it to work as expected
    if (i >= 8)     // sets the flag once length of array get over 8
      aFlag = 1;    
    i++;            // first increment index since we have already read at current index
    c = getchar();  //same as before
    ch[i] = c;
  }

  if (uFlag == 1 && cFlag == 1 && dFlag == 1 && hFlag == 1 && aFlag == 1)
  {
    printf("Password entered is valid.\n");
  }
  else
  {
    printf("Password entered is invalid.\n");
  }        
  return 0;
}

Hope this solves your problem !

Upvotes: 0

Stephan Lechner
Stephan Lechner

Reputation: 35154

char ch; defines a variable for a single character, not for a string. So you cannot use strlen(ch), because strlen expects a pointer to a string (and not a single character).

As you are reading in one character after another with ch = getchar() in a loop, you actually do not compose any string. The only thing you need to do is increment k with each iteration:

k = 0;
while (ch != '\n')  
  k++;
  ...
  ch = getchar();
}

Upvotes: 2

Related Questions