Skip to content
Related Articles

Related Articles

isalpha() and isdigit() functions in C with cstring examples.

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 31 Aug, 2022
Improve Article
Save Article

isalpha(c) is a function in C which can be used to check if the passed character is an alphabet or not. It returns a non-zero value if it’s an alphabet else it returns 0. For example, it returns non-zero values for ‘a’ to ‘z’ and ‘A’ to ‘Z’ and zeroes for other characters.
Similarly, isdigit(c) is a function in C which can be used to check if the passed character is a digit or not. It returns a non-zero value if it’s a digit else it returns 0. For example, it returns a non-zero value for ‘0’ to ‘9’ and zero for others.
Avoiding common errors : It is important to note this article does not cover strings! Only Cstrings. Cstrings are an array of single characters (char) in their behaviour. There are advantages and disadvantages to this.
Example Problem : Given a cstring str, find the number of alphabetic letters and number of decimal digits in that cstring.
Examples: 

Input: 12abc12
Output: Alphabetic_letters = 3, Decimal_digits = 4

Input: 123 GeeksForGeeks is Number 1
Output: Alphabetic_letters = 21, Decimal_digits = 4

Explanation And Approach: 

C




// C program to demonstrate working of isalpha() and
// isdigit().
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char str[] = "12abc12";
 
    int alphabet = 0, number = 0, i;
    for (i = 0; str[i] != '\0'; i++)
    {
     
        // check for alphabets
        if (isalpha(str[i]) != 0)
            alphabet++;
 
        // check for decimal digits
        else if (isdigit(str[i]) != 0)
            number++;
    }
 
    printf("Alphabetic_letters = %d, "
           "Decimal_digits = %d\n",
           alphabet, number);
 
    return 0;
}

Output: 

Alphabetic_letters = 3, Decimal_digits = 4

Time Complexity: O(n) where n is the size of the string.

Auxiliary Space: O(1)

Let us see the differences in a tabular form -:
 

 isalpha()isdigit()
1.It is used to check if the passed character is alphabetic. or not.It is used to check if the passed character is a decimal digit character.
2.

Its syntax is -:

isalpha(int c);

Its syntax is -:

isdigit(int c);

3.It takes only one parameter that is the character to be checked.It takes one parameter that is the character to be checked.
4.Its return value is non-zero value if c is an alphabet, else it returns 0.Its return value is non-zero value if c is a digit, else it returns 0.
5.It is defined in header file <ctype.h> It is defined in header file <ctype.h> 

This article is contributed by Mazhar Imam Khan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!