Check if a String Contains Special Characters in C

To check if a string contains special characters in C, you can use various approaches such as iterating through the string with the isalnum() function, comparing against a defined set of special characters using strchr(), or using the strpbrk() function from string.h.


Example 1: Using isalnum() to Check for Special Characters

In this example, we will iterate through the string and use the isalnum() function to check if each character is alphanumeric. If a character is not alphanumeric, it is considered a special character.

main.c

</>
Copy
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[] = "Hello@World";
    int foundSpecial = 0;

    for (int i = 0; str[i] != '\0'; i++) {
        if (!isalnum(str[i])) {
            foundSpecial = 1;
            break;
        }
    }

    if (foundSpecial)
        printf("Special character found!");
    else
        printf("No special characters found.");

    return 0;
}

Explanation:

  1. We include ctype.h to use the isalnum() function, which checks if a character is alphanumeric.
  2. The string str is initialized with "Hello@World".
  3. The variable foundSpecial is set to 0 to indicate no special characters have been found at the start.
  4. A for loop iterates through each character of str until the null terminator '\0' is reached.
  5. Inside the loop, if isalnum(str[i]) returns false (i.e. the character is not alphanumeric), foundSpecial is set to 1 and the loop terminates using break.
  6. An if statement checks the foundSpecial variable to print the appropriate message.

Output:

Special character found!

Example 2: Using a Defined Set of Special Characters with strchr()

In this example, we define a string containing special characters and use the strchr() function to determine if any character in our target string matches one from the defined set.

main.c

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

int main() {
    char str[] = "Welcome#Home";
    char specialChars[] = "!@#$%^&*()_+-=[]{}|;:',.<>/?";
    int found = 0;

    for (int i = 0; str[i] != '\0'; i++) {
        if (strchr(specialChars, str[i]) != NULL) {
            found = 1;
            break;
        }
    }

    if (found)
        printf("Special character detected!");
    else
        printf("No special characters detected.");

    return 0;
}

Explanation:

  1. We include string.h to use the strchr() function which searches for a character in a string.
  2. The string str is set to "Welcome#Home" and the specialChars string contains a list of special characters.
  3. A for loop iterates over each character in str until the end of the string.
  4. The function strchr(specialChars, str[i]) checks if the current character exists in specialChars.
  5. If a match is found, the variable found is set to 1 and the loop exits.
  6. An if statement then prints the corresponding message based on whether a special character was detected.

Output:

Special character detected!

Example 3: Using strpbrk() to Find Special Characters

In this example, we use the strpbrk() function to search for the first occurrence of any special character from a predefined set within the string.

main.c

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

int main() {
    char str[] = "Data123$Info";
    char specialSet[] = "!@#$%%^&*";
    
    // strpbrk returns a pointer to the first occurrence of any character from specialSet in str
    char *result = strpbrk(str, specialSet);
    
    if (result != NULL)
        printf("Special character '%c' found!", *result);
    else
        printf("No special characters found.");
    
    return 0;
}

Explanation:

  1. We include string.h to access the strpbrk() function.
  2. The string str is initialized to "Data123$Info", and specialSet contains the special characters to look for.
  3. strpbrk(str, specialSet) scans str for any character that matches one in specialSet and returns a pointer to the first occurrence.
  4. If the returned pointer result is not NULL, it indicates that a special character was found, which is then printed.
  5. If result is NULL, the program prints that no special characters were found.

Output:

Special character '$' found!

Conclusion

In this tutorial, we explored three different methods to check if a string contains special characters in C:

  1. isalnum(): Iterates through the string and identifies non-alphanumeric characters.
  2. strchr(): Uses a defined set of special characters to detect any matches in the string.
  3. strpbrk(): Searches for the first occurrence of any special character from a predefined set.