Tutorialspoint.dev

isalpha() and isdigit() functions in C/C++ with example

isalpha(c) is a function in C which can be used to check if 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 zero for other characters.

Similarly, isdigit(c) is a function in C which can be used to check if 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 non-zero value for ‘0’ to ‘9’ and zero for others.

Example Problem : Given a string str, find the number of alphabetic letters and number of decimal digits in that string.

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 program to demonstrate working of isalpha() and
// isdigit().
#include<stdio.h>
#include<stdlib.h>
  
int main()
{
    char str[] = "12abc12";
  
    int alphabet = 0, number = 0, i;
    for (i=0; str[i]!= ''; 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 ", alphabet, number);
  
    return 0;
}

Output:

Alphabetic_letters = 3, Decimal_digits = 4

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter