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
#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:
- We include
ctype.h
to use theisalnum()
function, which checks if a character is alphanumeric. - The string
str
is initialized with"Hello@World"
. - The variable
foundSpecial
is set to0
to indicate no special characters have been found at the start. - A
for
loop iterates through each character ofstr
until the null terminator'\0'
is reached. - Inside the loop, if
isalnum(str[i])
returns false (i.e. the character is not alphanumeric),foundSpecial
is set to1
and the loop terminates usingbreak
. - An
if
statement checks thefoundSpecial
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
#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:
- We include
string.h
to use thestrchr()
function which searches for a character in a string. - The string
str
is set to"Welcome#Home"
and thespecialChars
string contains a list of special characters. - A
for
loop iterates over each character instr
until the end of the string. - The function
strchr(specialChars, str[i])
checks if the current character exists inspecialChars
. - If a match is found, the variable
found
is set to1
and the loop exits. - 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
#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:
- We include
string.h
to access thestrpbrk()
function. - The string
str
is initialized to"Data123$Info"
, andspecialSet
contains the special characters to look for. strpbrk(str, specialSet)
scansstr
for any character that matches one inspecialSet
and returns a pointer to the first occurrence.- If the returned pointer
result
is notNULL
, it indicates that a special character was found, which is then printed. - If
result
isNULL
, 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: