Check if a String Contains Non-ASCII Characters in C
In this tutorial, you will learn how to check if a string contains non-ASCII characters in C. We will explore multiple approaches to detect characters outside the ASCII range (0 to 127) using different functions and methods.
Example 1: Checking Non-ASCII Characters using a Simple Loop
In this example, we will use a simple for
loop to iterate through each character in a string and check if its ASCII value is greater than 127. If any character exceeds this value, we determine that the string contains non-ASCII characters.
main.c
#include <stdio.h>
int main() {
char str[] = "Hello, Wörld!"; // String with a non-ASCII character (ö)
int i;
int flag = 0;
// Check each character in the string
for (i = 0; str[i] != '\0'; i++) {
if ((unsigned char)str[i] > 127) {
flag = 1;
break;
}
}
if (flag) {
printf("The string contains non-ASCII characters.\n");
} else {
printf("The string contains only ASCII characters.\n");
}
return 0;
}
Explanation:
- The string
str
is initialized with “Hello, Wörld!”, which includes the non-ASCII characterö
. - A
for
loop iterates over the string until the null terminator'\0'
is encountered. - Each character is cast to
unsigned char
and its ASCII value is compared with 127. - If a character’s ASCII value is greater than 127, the flag variable is set to 1 and the loop breaks.
- After the loop, the flag is checked to determine whether to print a message indicating the presence of non-ASCII characters.
Output:
The string contains non-ASCII characters.
Example 2: Checking Non-ASCII Characters using the isascii
Function
In this example, we will use the isascii
function from the <ctype.h>
library to check whether each character in the string is an ASCII character. The function returns a non-zero value if the character is within the ASCII range and 0 otherwise.
main.c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello, 世界!"; // String with non-ASCII characters (世界)
int i;
int flag = 0;
// Iterate over each character and use isascii() to check
for (i = 0; str[i] != '\0'; i++) {
if (!isascii((unsigned char)str[i])) {
flag = 1;
break;
}
}
if (flag) {
printf("The string contains non-ASCII characters.\n");
} else {
printf("The string contains only ASCII characters.\n");
}
return 0;
}
Explanation:
- The string
str
is defined as “Hello, 世界!” which includes non-ASCII characters (the Chinese characters for “world”). - A
for
loop is used to traverse the string until the null terminator'\0'
is reached. - The
isascii
function, after casting each character tounsigned char
, checks if the character is within the ASCII range. - If
isascii
returns 0 for any character, the flag variable is set to 1 and the loop terminates early. - Finally, the flag is evaluated to print the appropriate message indicating whether non-ASCII characters are present.
Output:
The string contains non-ASCII characters.
Conclusion
In this tutorial, we learned two different methods to check if a string contains non-ASCII characters in C. The first method manually compares each character’s ASCII value using a loop, while the second method leverages the isascii
function from the <ctype.h>
library for clarity and simplicity.