25 C
Jaipur
Friday, October 23, 2020

How to Use isalpha() in C Language – Linux Hint

Must read

huawei: Huawei ekes out third-quarter revenue growth as US restrictions bite – Latest News

Huawei Technologies Co Ltd eked out a gain in third-quarter revenue as the impact of the COVID-19 pandemic added to supply-chain difficulties brought about...

MEE Audio launches new range of Bluetooth audio accessories

MEE Audio has released a new range of audio products at different price points. There’s a new ‘MEE Audio Connect

Accenture along with SAP aims to take businesses into cloud-based open industry solutions

Accenture has announced it will be working side-by-side with SAP to help companies change their business operations with industry-specific solutions based on SAP’s...

huawei: Italy vetoes 5G deal between Fastweb and China’s Huawei: Sources – Latest News

Italy has prevented telecoms group Fastweb from signing a supply deal with Huawei for its 5G core network, two sources close to the matter...

There are several standard library header files in the C programming language used to perform various standard operations. The “ctype.h” is one such header file, and the “isalpha()” function is one of the library functions provided by “ctype.h.” The isalpha() library function is used to identify whether a character is an alphabet. In this article, you will learn about the isalpha() library function in C language.

Prototype of isalpha()

This is the prototype for the function in C programming language:

Understanding isalpha()

The isalpha() function is a library function provided by “ctype.h.” This function checks whether a character is an alphabet character. If the function detects that the input character is an alphabet character (‘A’ to ‘Z’ or ‘a’ to ‘z’), it returns a non-zero integer value. But if the input character is not an alphabet character, then the function returns zero.

If you look closely at the function prototype mentioned above, the function takes one argument of the integer type. However, when we call the isaplha() function, we pass a character (‘A’ to ‘Z’ or ‘a’ to ‘z’). The value of the character is converted into an integer value. In C language, a character is stored in the memory as the corresponding ASCII value. Each alphabet has a corresponding ASCII value. For example, the ASCII value for “A” is 65, “b” is 98, etc.

Note: ASCII stands for American Standard Code for Information Interchange. The complete ASCII table can be found at the following address:

https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

Examples

Now that you understand the isalpha() function and its syntax, let us look at a few examples:

  • Example 1: Upper-Case Alphabets
  • Example 2: Lower-Case Alphabets
  • Example 3: Digits
  • Example 4: Special Characters
  • Example 5: Practical Usage

Example 1: Upper-Case Alphabets

In this example, you will see how the isalpha() function detects upper-case alphabets.

#include <stdio.h>
#include <ctype.h>
   
int main()
{
    char char_input_1 = ‘A’;
    char char_input_2 = ‘B’;
    char char_input_3 = ‘M’;
    char char_input_4 = ‘Y’;
    char char_input_5 = ‘Z’;
   
    /* Check if char_input_1 is an alphabet */
    if(isalpha(char_input_1))
        printf(“%c is an alphabet.n,char_input_1);
    else
        printf(“%c is not an alphabet.n,char_input_1);
       
       
    /* Check if char_input_2 is an alphabet */
    if(isalpha(char_input_2))
        printf(“%c is an alphabet.n,char_input_2);
    else
        printf(“%c is not an alphabet.n,char_input_2);
       
       
    /* Check if char_input_3 is an alphabet */
    if(isalpha(char_input_3))
        printf(“%c is an alphabet.n,char_input_3);
    else
        printf(“%c is not an alphabet.n,char_input_3);
       
       
    /* Check if char_input_4 is an alphabet */
    if(isalpha(char_input_4))
        printf(“%c is an alphabet.n,char_input_4);
    else
        printf(“%c is not an alphabet.n,char_input_4);
       
       
    /* Check if char_input_5 is an alphabet */
    if(isalpha(char_input_5))
        printf(“%c is an alphabet.n,char_input_5);
    else
        printf(“%c is not an alphabet.n,char_input_5);
   
       
    return 0;
}

Example 2: Lower-Case Alphabets

In this example, you will see how the isalpha() function detects lower-case alphabets and returns a non-zero integer value.

#include <stdio.h>
#include <ctype.h>
   
int main()
{
    char char_input_1 = ‘a’;
    char char_input_2 = ‘b’;
    char char_input_3 = ‘m’;
    char char_input_4 = ‘y’;
    char char_input_5 = ‘z’;
   
    /* Check if char_input_1 is an alphabet */
    if(isalpha(char_input_1))
        printf(“%c is an alphabet.n,char_input_1);
    else
        printf(“%c is not an alphabet.n,char_input_1);
       
       
    /* Check if char_input_2 is an alphabet */
    if(isalpha(char_input_2))
        printf(“%c is an alphabet.n,char_input_2);
    else
        printf(“%c is not an alphabet.n,char_input_2);
       
       
    /* Check if char_input_3 is an alphabet */
    if(isalpha(char_input_3))
        printf(“%c is an alphabet.n,char_input_3);
    else
        printf(“%c is not an alphabet.n,char_input_3);
       
       
    /* Check if char_input_4 is an alphabet */
    if(isalpha(char_input_4))
        printf(“%c is an alphabet.n,char_input_4);
    else
        printf(“%c is not an alphabet.n,char_input_4);
       
       
    /* Check if char_input_5 is an alphabet */
    if(isalpha(char_input_5))
        printf(“%c is an alphabet.n,char_input_5);
    else
        printf(“%c is not an alphabet.n,char_input_5);
   
       
    return 0;
}

Example 3: Digits

In this example, you will see that the isalpha() function returns zero when we pass numerical digits.

#include <stdio.h>
#include <ctype.h>
   
int main()
{
    char char_input_1 = ‘0’;
    char char_input_2 = ‘1’;
    char char_input_3 = ‘2’;
    char char_input_4 = ‘3’;
    char char_input_5 = ‘4’;
   
    /* Check if char_input_1 is an alphabet */
    if(isalpha(char_input_1))
        printf(“%c is an alphabet.n,char_input_1);
    else
        printf(“%c is not an alphabet.n,char_input_1);
       
       
    /* Check if char_input_2 is an alphabet */
    if(isalpha(char_input_2))
        printf(“%c is an alphabet.n,char_input_2);
    else
        printf(“%c is not an alphabet.n,char_input_2);
       
       
    /* Check if char_input_3 is an alphabet */
    if(isalpha(char_input_3))
        printf(“%c is an alphabet.n,char_input_3);
    else
        printf(“%c is not an alphabet.n,char_input_3);
       
       
    /* Check if char_input_4 is an alphabet */
    if(isalpha(char_input_4))
        printf(“%c is an alphabet.n,char_input_4);
    else
        printf(“%c is not an alphabet.n,char_input_4);
       
       
    /* Check if char_input_5 is an alphabet */
    if(isalpha(char_input_5))
        printf(“%c is an alphabet.n,char_input_5);
    else
        printf(“%c is not an alphabet.n,char_input_5);
   
       
    return 0;
}

Example 4: Special Characters

In this example, you will see that the isalpha() function returns zero when we pass special characters.

#include <stdio.h>
#include <ctype.h>
   
int main()
{
    char char_input_1 = ‘&’;
    char char_input_2 = ‘$’;
    char char_input_3 = ‘#’;
    char char_input_4 = ‘%’;
    char char_input_5 = ‘@’;
   
    /* Check if char_input_1 is an alphabet */
    if(isalpha(char_input_1))
        printf(“%c is an alphabet.n,char_input_1);
    else
        printf(“%c is not an alphabet.n,char_input_1);
       
       
    /* Check if char_input_2 is an alphabet */
    if(isalpha(char_input_2))
        printf(“%c is an alphabet.n,char_input_2);
    else
        printf(“%c is not an alphabet.n,char_input_2);
       
       
    /* Check if char_input_3 is an alphabet */
    if(isalpha(char_input_3))
        printf(“%c is an alphabet.n,char_input_3);
    else
        printf(“%c is not an alphabet.n,char_input_3);
       
       
    /* Check if char_input_4 is an alphabet */
    if(isalpha(char_input_4))
        printf(“%c is an alphabet.n,char_input_4);
    else
        printf(“%c is not an alphabet.n,char_input_4);
       
       
    /* Check if char_input_5 is an alphabet */
    if(isalpha(char_input_5))
        printf(“%c is an alphabet.n,char_input_5);
    else
        printf(“%c is not an alphabet.n,char_input_5);
   
       
    return 0;
}

Example 5: Practical Usage

In this example, we will look into the practical usage of the isalpha() function in a real-world situation. Suppose that we are receiving an input character stream and we need to extract the meaningful alphabets from it. We can use the islpha() function to extract the alphabets from the input stream.

#include <stdio.h>
#include <ctype.h>
   
   
int main()
{
    char char_input[] = “5673&^%_SOF2*!”;
    char char_output[10];
    int i = 0, j = 0;
   
    while(char_input[i] != )
    {
        if(isalpha(char_input[i]))
        {
            char_output[j] = char_input[i];
            j++;
        }
        i++;
    }
    char_output[j] = ;
   
    printf(“char_output = %sn,char_output);  
       
    return 0;
}

Conclusion

In multiple examples of the practical usage of the isalpha() function, this article showed you how the isalpha() function plays a key role in detecting alphabet characters in the C programming language. This function is mainly used in embedded programming, where we receive a stream of characters and we need to extract meaningful alphabets from the input stream.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

huawei: Huawei ekes out third-quarter revenue growth as US restrictions bite – Latest News

Huawei Technologies Co Ltd eked out a gain in third-quarter revenue as the impact of the COVID-19 pandemic added to supply-chain difficulties brought about...

MEE Audio launches new range of Bluetooth audio accessories

MEE Audio has released a new range of audio products at different price points. There’s a new ‘MEE Audio Connect

Accenture along with SAP aims to take businesses into cloud-based open industry solutions

Accenture has announced it will be working side-by-side with SAP to help companies change their business operations with industry-specific solutions based on SAP’s...

huawei: Italy vetoes 5G deal between Fastweb and China’s Huawei: Sources – Latest News

Italy has prevented telecoms group Fastweb from signing a supply deal with Huawei for its 5G core network, two sources close to the matter...

huawei: Huawei reports 9.9% revenue growth in first 3 quarters of 2020 – Latest News

Huawei on Friday said that it generated a revenue of 671.3 billion yuan ($98.57 billion) in the first three quarters of this year, an...