I'm making my first "big project" in C and I'm currently working on the registration/login part. I would like to make the user insert an efficient password, asking him to insert characters and numbers (I'm not sure if i simply want to print a warning if the inserted password is unsafe or making the user repeat the inserting process but that's not important now). The password will be part of a struct called "user". So, in order to do this, how do i scan an input made of characters and numbers like "hfsk89skh3"??
Asked
Active
Viewed 633 times
1
-
1If this is a teaching project and you're still using `scanf`, `%s` should do. (Although your user will then be limited to passwords not containing spaces.) Or you could use `fgets`, although you'll have to [strip the newline](https://stackoverflow.com/questions/2693776). If you want to enforce properties of the password (like the length, or the characters it may or may not contain), do that later. – Steve Summit Jun 27 '21 at 11:53
-
By *scan*, do you mean using `scanf()` to input the string ("%s" will do), or to you mean examining all characters and count how many for each category (alpha, num, punctuation)? – fpiette Jun 27 '21 at 12:01
-
It might be helpful not to think of `'0'` thru `'9'` as numbers but as single *digits*. Are you allowed to use library functions in `ctype.h` such as `isdigit()` and `isalpha()` etc? See [character classification](https://learn.microsoft.com/en-us/cpp/c-runtime-library/character-classification?view=msvc-160). – Weather Vane Jun 27 '21 at 12:14
-
Another possibility -- although for a number of reasons I do not recommend it -- is `scanf(" %[a-zA-Z0-9]")`. – Steve Summit Jun 27 '21 at 12:18
3 Answers
0
In C a string can contain alphanumeric characters, so if you scan a string and input this value("hfsk89skh3") that shouldn't be a problem

Katout999
- 61
- 4
0
If your goal is to check whether a password contains both letters and numbers, I recommend using isaplha() and isdigit()
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[] = "12abc12";
int alphabet = 0, number = 0, i;
//cycle through every character of the string
for (i = 0; str[i] != '\0'; i++)
{
// check for letters (returns 0 if not a letter)
if (isalpha(str[i]) != 0)
alphabet++;
// check for decimal digits (returns 0 if not a digit)
else if (isdigit(str[i]) != 0)
number++;
}
//check if you have at least one number and 5 letters
if((alphabet>5)&&(number>0)){
printf("Valid Password")
}
return 0;
}

Andrea Zaffanella
- 20
- 2
-
Thanks everyone, especially @andrea zaffanella, very useful piece of code bro – Tiziano666 Jun 27 '21 at 16:25
-
@Tiziano666 No problem! If this was useful to you may you add it as the solution? Thank you – Andrea Zaffanella Jul 17 '21 at 19:47
0
You can do a simple for loop that iterates over their password and turn on a flag for each condition that you want to check for. Then at the end, if not all of the flags are on, you tell them that the password isn't safe.

Nameles36
- 63
- 6