cybersecurity · beginner · ~15 min

Does it contain a special character?

Detect non-alphanumeric characters.

Challenge

Detect whether a password contains a non-alphanumeric character.

Task

Implement int has_special(const char *pw) that returns 1 if pw contains at least one character that is not a letter or a digit, else 0.

Input

  • pw: a NUL-terminated password string the grader passes.

Output

Returns int: 1 if any character is outside [A-Za-z0-9], else 0.

Example

has_special("abc!23")   ->   1   (the '!')
has_special("abc123")   ->   0

Edge cases

  • A string with only letters and digits returns 0.

Input format

A NUL-terminated password string pw.

Output format

An int: 1 if pw has a character outside [A-Za-z0-9], else 0.

Constraints

A character that is not a letter or digit counts as special.

Starter code

int has_special(const char *pw) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.