Home > Enterprise >  How do I check for a specific character after a user inputed a string? [closed]
How do I check for a specific character after a user inputed a string? [closed]

Time:09-21

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.h for the exit(..) function. This function needs an argument, and since it is used to exit with failure, the best choice is EXIT_FAILURE as defined in stdlib.h. Since exit(..) is called in the main function, another option is to simply return EXIT_FAILURE.
  • The scanf_s function is a bit of a special case: __STDC_WANT_LIB_EXT1__ needs to be defined as the integer constant 1 before stdio.h is 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, fgets is a better alternative.
  • The function scanf_s needs a char array to write to and password has exactly that type. Therefore, the "address of" operator (&) shouldn't be used here.
  • Then function strchr needs a char argument ('!'), 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_t type for lengths. This is also the type that is returned by strlen.
  • Declare variables as close as possible to where these are first used.
  • Store the result of strchr directly into a boolean value. To use the bool type, include stdbool.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);
}
  •  Tags:  
  • c
  • Related