YouTip LogoYouTip

C Function Isalpha

## C Library Function - isalpha() The `isalpha()` function is a built-in C library function used to check if a given character is an alphabetic letter (either uppercase or lowercase). This function is declared in the `` header file. --- ## Syntax and Declaration Below is the prototype for the `isalpha()` function: ```c int isalpha(int c); ``` ### Parameters * **`c`**: This is the character to be checked. Although the parameter is of type `int`, you pass a character (or its ASCII value) to the function. The value of `c` must be representable as an `unsigned char` or equal to `EOF`. ### Return Value * **Non-zero value (True)**: If the character `c` is an alphabetic letter (A-Z or a-z). * **Zero (0) (False)**: If the character `c` is not an alphabetic letter (e.g., digits, punctuation, whitespace, or control characters). --- ## How isalpha() Works In the default `"C"` locale, `isalpha(c)` is equivalent to: * `isupper(c)` or `islower(c)` This means it returns true for characters within the following ASCII ranges: * Uppercase letters: `'A'` to `'Z'` (ASCII values 65 to 90) * Lowercase letters: `'a'` to `'z'` (ASCII values 97 to 122) *Note: In other locales, additional characters may be considered alphabetic.* --- ## Code Example The following program demonstrates how to use the `isalpha()` function to check different types of characters. ```c #include #include int main() { int var1 = 'd'; int var2 = '2'; int var3 = '\t'; int var4 = ' '; // Check if var1 ('d') is alphabetic if( isalpha(var1) ) { printf("var1 = |%c| is an alphabetic character\n", var1 ); } else { printf("var1 = |%c| is NOT an alphabetic character\n", var1 ); } // Check if var2 ('2') is alphabetic if( isalpha(var2) ) { printf("var2 = |%c| is an alphabetic character\n", var2 ); } else { printf("var2 = |%c| is NOT an alphabetic character\n", var2 ); } // Check if var3 ('\t') is alphabetic if( isalpha(var3) ) { printf("var3 = |\\t| is an alphabetic character\n" ); } else { printf("var3 = |\\t| is NOT an alphabetic character\n" ); } // Check if var4 (' ') is alphabetic if( isalpha(var4) ) { printf("var4 = |%c| is an alphabetic character\n", var4 ); } else { printf("var4 = |%c| is NOT an alphabetic character\n", var4 ); } return 0; } ``` ### Output When you compile and run the program above, it will produce the following output: ```text var1 = |d| is an alphabetic character var2 = |2| is NOT an alphabetic character var3 = |\t| is NOT an alphabetic character var4 = | | is NOT an alphabetic character ``` --- ## Practical Application: Counting Letters in a String A common real-world use case for `isalpha()` is validating user input or parsing text. The example below demonstrates how to count the number of alphabetic letters in a string. ```c #include #include int main() { char str[] = "Hello, World! 2023."; int letter_count = 0; for (int i = 0; str != '\0'; i++) { if (isalpha((unsigned char)str)) { letter_count++; } } printf("The string is: \"%s\"\n", str); printf("Number of alphabetic letters: %d\n", letter_count); return 0; } ``` ### Output ```text The string is: "Hello, World! 2023." Number of alphabetic letters: 10 ``` --- ## Important Considerations 1. **Undefined Behavior with `char`**: The `isalpha()` function expects an `int` that can be represented as an `unsigned char`. If you pass a standard signed `char` directly, characters with negative ASCII values (like accented characters in extended ASCII) can cause undefined behavior. To prevent this, always cast the argument to `unsigned char`: ```c isalpha((unsigned char)c); ``` 2. **Locale Dependency**: The behavior of `isalpha()` depends on the current system locale. In non-default locales, characters like `Γ‘`, `ΓΆ`, or `Γ§` might also be classified as alphabetic.
← C Function IscntrlC Function Isalnum β†’