In C, the isalpha() function is used to determine whether a given character is an alphabet or not. It is defined in the <ctype.h> header file and is part of the C standard library.
The isalpha() function takes an integer or character as an argument and returns an integer value. It returns a non-zero value (usually 1) if the character is an alphabet, and 0 otherwise.
Here’s the syntax of the isalpha() function:
<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><ctype.h></span></span>
<span class="hljs-type">int</span> <span class="hljs-title function_">isalpha</span><span class="hljs-params">(<span class="hljs-type">int</span> c)</span>;
- The
cparameter represents the character that you want to test.
The isalpha() function checks if the character is an uppercase letter (A-Z) or a lowercase letter (a-z). It does not consider any other characters such as digits or special symbols.
Here’s an example usage of isalpha():
<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><stdio.h></span></span>
<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><ctype.h></span></span>
<span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span> {
<span class="hljs-type">char</span> ch = <span class="hljs-string">'A'</span>;
<span class="hljs-keyword">if</span> (<span class="hljs-built_in">isalpha</span>(ch)) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c is an alphabet.\n"</span>, ch);
} <span class="hljs-keyword">else</span> {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c is not an alphabet.\n"</span>, ch);
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
A <span class="hljs-keyword">is</span> an alphabet.
In the example above, the isalpha() function is used to check if the character 'A' is an alphabet. Since it is an uppercase letter, the function returns a non-zero value, and the program prints that it is an alphabet.
