I am a new developer to C and I am trying to make a password detector and I am trying to code something that reads the users password and checks if it has a "!" in it. Yet, I cant seem to get it to work. the output of "int special" always equals 0. The code:
#include <stdio.h>
#include <string.h>
void main() {
// check for special characters
// check for length of password
char password[30];
int length;
int len = 15;
printf("Dear user please enter a password:\n ");
scanf_s("%s", &password, 30);
length = strlen(password);
if (length < len) {
printf("invalid password (password must be 15 - 30 characters)");
exit();
}
int special = 0;
if (strchr(password, "!") != NULL)
{
special = 1;
}
printf("%d", special);
}
CodePudding user response:
The basic idea of the program is correct, but a couple of fixes are needed to make it work and standard compliant:
- The typical signature for the main function, using no parameters, is
int main(void). See the C standard document for more information. - Include
stdlib.hfor theexit(..)function. This function needs an argument, and since it is used to exit with failure, the best choice isEXIT_FAILUREas defined instdlib.h. Sinceexit(..)is called in the main function, another option is to simply returnEXIT_FAILURE. - The
scanf_sfunction is a bit of a special case:__STDC_WANT_LIB_EXT1__needs to be defined as the integer constant1beforestdio.his included. See here for more information. Note that providing an implementation for this function is optional in C, so it is not the best choice for portability. Here,fgetsis a better alternative. - The function
scanf_sneeds achararray to write to andpasswordhas exactly that type. Therefore, the "address of" operator (&) shouldn't be used here. - Then function
strchrneeds acharargument ('!'), not a string literal ("!"). See here for the exact function prototype.
The code will all fixes applied:
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
// check for special characters
// check for length of password
char password[30];
int length;
int len = 15;
printf("Dear user please enter a password:\n");
scanf_s("%s", password, 30);
length = strlen(password);
if (length < len) {
printf("invalid password (password must be 15 - 30 characters)\n");
exit(EXIT_FAILURE);
}
int special = 0;
if (strchr(password, '!') != NULL)
{
special = 1;
}
printf("%d\n", special);
}
A few additional suggestions:
- Use a macro for the constant length to give it a name, and to define it once and reuse it multiple times.
- Use the
size_ttype for lengths. This is also the type that is returned bystrlen. - Declare variables as close as possible to where these are first used.
- Store the result of
strchrdirectly into a boolean value. To use thebooltype, includestdbool.h.
The improved code:
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PASSWORD_LENGTH_MAX 30U
int main(void) {
char password[PASSWORD_LENGTH_MAX];
printf("Dear user please enter a password:\n");
scanf_s("%s", password, PASSWORD_LENGTH_MAX);
size_t length_min = 15U;
size_t length = strlen(password);
if (length < length_min) {
printf("Invalid password (password must be 15 - 30 characters)\n");
exit(EXIT_FAILURE);
}
bool contains_special_character = strchr(password, '!');
printf("%d\n", contains_special_character);
}
