Program to check if a string contains any special character in C

In C programming, checking if a string contains special characters is a common validation task. Special characters are those that are neither alphabetic nor numeric, such as symbols like !@#$%^&*() etc.

Syntax

int checkSpecialCharacter(char str[], int length);

Method 1: Using Character Comparison

This method iterates through each character and compares it against a predefined set of special characters −

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

int checkSpecialCharacter(char str[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        if (str[i] == '!' || str[i] == '@' || str[i] == '#' || str[i] == '$'
        || str[i] == '%' || str[i] == '^' || str[i] == '&' || str[i] == '*'
        || str[i] == '(' || str[i] == ')' || str[i] == '-' || str[i] == '+'
        || str[i] == '=' || str[i] == '{' || str[i] == '}' || str[i] == '['
        || str[i] == ']' || str[i] == ':' || str[i] == ';' || str[i] == '"'
        || str[i] == ''' || str[i] == '<' || str[i] == '>' || str[i] == '.'
        || str[i] == '/' || str[i] == '?' || str[i] == '~' || str[i] == '`') {
            return 1; /* Special character found */
        }
    }
    return 0; /* No special character */
}

int main() {
    char str1[] = "tutorialspoint";
    char str2[] = "tutorials-point";
    
    printf("String 1: %s<br>", str1);
    if (checkSpecialCharacter(str1, strlen(str1))) {
        printf("The string is not accepted<br><br>");
    } else {
        printf("The string is accepted<br><br>");
    }
    
    printf("String 2: %s<br>", str2);
    if (checkSpecialCharacter(str2, strlen(str2))) {
        printf("The string is not accepted<br>");
    } else {
        printf("The string is accepted<br>");
    }
    
    return 0;
}
String 1: tutorialspoint
The string is accepted

String 2: tutorials-point
The string is not accepted

Method 2: Using ASCII Values

This approach checks if characters fall outside the alphanumeric range using ASCII values −

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

int isAlphanumeric(char c) {
    return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'));
}

int checkSpecialCharacterASCII(char str[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        if (!isAlphanumeric(str[i])) {
            return 1; /* Special character found */
        }
    }
    return 0; /* No special character */
}

int main() {
    char str1[] = "Hello123";
    char str2[] = "Hello@123";
    
    printf("String 1: %s<br>", str1);
    if (checkSpecialCharacterASCII(str1, strlen(str1))) {
        printf("The string is not accepted<br><br>");
    } else {
        printf("The string is accepted<br><br>");
    }
    
    printf("String 2: %s<br>", str2);
    if (checkSpecialCharacterASCII(str2, strlen(str2))) {
        printf("The string is not accepted<br>");
    } else {
        printf("The string is accepted<br>");
    }
    
    return 0;
}
String 1: Hello123
The string is accepted

String 2: Hello@123
The string is not accepted

Comparison

Method Pros Cons Time Complexity
Character Comparison Specific control over special chars Longer code, manual character list O(n)
ASCII Values Shorter code, automatic detection Less control over specific characters O(n)

Conclusion

Both methods effectively detect special characters in strings. The ASCII approach is more concise and automatically handles all non-alphanumeric characters, while character comparison gives precise control over which characters to consider special.

Updated on: 2026-03-15T12:56:48+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements