Character Functions

In this tutorial, we will learn about various character functions in the C programming language that we can use whenever we need them.


The Importance of Character Functions

Let’s consider the following program that converts a lowercase letter to uppercase:

#include <stdio.h>

int main() {
 char ch = 'a';
 if (ch >= 'a' && ch <= 'z') {
     ch = ch - 'a' + 'A';
 }
 printf("%c", ch);
 return 0;
}

Output:

A

Explanation:
In this program, we have a variable named “ch” of type char, which is assigned the value ‘a’. We use an if statement to check if the character stored in “ch” is within the range of lowercase letters. If it is, we execute the code inside the if block, enclosed in curly braces. In this case, since “ch” holds the character ‘a’, the statement ch = ch - 'a' + 'A' will be executed.

Recalling that the ASCII value of ‘a’ is 97 and that of ‘A’ is 65, the statement ch = ch - 'a' + 'A' will be replaced by ch = 97 - 97 + 65, resulting in “ch” holding the ASCII value of the character ‘A’. Therefore, “ch” will hold the character ‘A’, effectively converting the lowercase letter ‘a’ to uppercase.

To verify this, let’s see what happens when “ch” holds the character ‘c’.

#include <stdio.h>

int main() {
 char ch = 'c';
 if (ch >= 'a' && ch <= 'z') {
     ch = ch - 'a' + 'A';
 }
 printf("%c", ch);
 return 0;
}

Output:

C

Explanation:
The output is ‘C’ because the ASCII value of ‘c’ is 99, and 99 – 97 = 2. Adding 2 to the ASCII value of ‘A’ gives 67, which is the ASCII value of ‘C’, resulting in the desired uppercase letter.

We can simplify the process of converting lowercase letters to uppercase by using the toupper() function, which is provided for us to use. The updated program would look like this:

#include <stdio.h>
#include <ctype.h>

int main() {
 char ch = 'a';
 ch = toupper(ch);
 printf("%c", ch);
 return 0;
}

Output:

A

Explanation:

The output remains the same as before. However, instead of implementing our own logic, we utilize the toupper() function to convert the lowercase letter ‘a’ to the uppercase letter ‘A’. By calling the toupper() function and passing “ch” as an argument, we provide the input for the function. The function performs the necessary operations behind the scenes and returns the uppercase ‘A’, which is then stored in the variable “ch”.

Note that the toupper() function is available in the ctype.h header file.

The program above clearly demonstrates the usefulness of character functions, as they provide convenience and allow programmers to focus on the main logic of their programs by leveraging the power of predefined functions like these.

As you may have guessed, there are many more character handling functions available in the ctype.h header file. Below, we list some commonly used ones:


isalnum(char c)

The isalnum(char c) function checks if the given character c is alphanumeric (an alphabet or a digit). It returns a non-zero value (true) if c is alphanumeric, else it returns 0 (false).

Example:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'A';

    if (isalnum(ch)) {
        printf("%c is alphanumeric.\n", ch);
    } else {
        printf("%c is not alphanumeric.\n", ch);
    }

    return 0;
}

Output:

A is alphanumeric.

Explanation:
The character ‘A’ is indeed alphanumeric, so the condition in the if statement is true. Therefore, the statement inside the if block is executed, resulting in the output “A is alphanumeric.”

Now, let’s replace char ch = 'A'; with char ch = '@'; and observe the result:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = '@';

    if (isalnum(ch)) {
        printf("%c is alphanumeric.\n", ch);
    } else {
        printf("%c is not alphanumeric.\n", ch);
    }

    return 0;
}

Output:

@ is not alphanumeric.

Explanation
The character ‘@’ is not alphanumeric since it is neither an alphabet nor a digit. Hence, the condition in the if statement is false, and the else block is executed, resulting in the output “@ is not alphanumeric.”

The concept of if-else should be clear from the above examples. When the condition within the parentheses of the if statement evaluates to false, the code within the else block is executed. We will delve deeper into the concept of if-else in our future tutorials.


isalpha(char c)

The isalpha(char c) function checks if the given character c is an alphabet (uppercase or lowercase). It returns a non-zero value (true) if c is an alphabet, else it returns 0 (false).

Example:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = '9';

    if (isalpha(ch)) {
        printf("%c is an alphabet.\n", ch);
    } else {
        printf("%c is not an alphabet.\n", ch);
    }

    return 0;
}

Output:

9 is not an alphabet.

Explanation:
The program and the output is self-explanatory.


isdigit(char c)

The isdigit(char c) function checks if the given character c is a digit (0-9). It returns a non-zero value (true) if c is a digit, else it returns 0 (false).

Example:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = '5';

    if (isdigit(ch)) {
        printf("%c is a digit.\n", ch);
    } else {
        printf("%c is not a digit.\n", ch);
    }

    return 0;
}

Output:

5 is a digit.

islower(char c)

The islower(char c) function checks if the given character c is a lowercase letter (a-z). It returns a non-zero value (true) if c is a lowercase letter, else it returns 0 (false).

Example:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'k';

    if (islower(ch)) {
        printf("%c is a lowercase alphabet.\n", ch);
    } else {
        printf("%c is not a lowercase alphabet.\n", ch);
    }

    return 0;
}

Output:

k is a lowercase alphabet.

isupper(char c)

The isupper(char c) function checks if the given character c is an uppercase letter (A-Z). It returns a non-zero value (true) if c is an uppercase letter, else it returns 0 (false).

Example:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'F';

    if (isupper(ch)) {
        printf("%c is an uppercase alphabet.\n", ch);
    } else {
        printf("%c is not an uppercase alphabet.\n", ch);
    }

    return 0;
}

Output:

F is an uppercase alphabet.

tolower(char c)

The tolower(char c) function converts the given uppercase alphabet stored in variable c to lowercase. It returns the lowercase equivalent of c if c is an uppercase alphabet, otherwise it returns c itself.

Example:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'G';
    char lowercase_ch = tolower(ch);

    printf("The lowercase equivalent of %c is %c.\n", ch, lowercase_ch);

    return 0;
}

Output:

The lowercase equivalent of G is g.

These are just a few commonly used character functions. For more function, visit this website.


Summary

In this tutorial, we have introduced ourselves to various functions that can be used for character handling in the C programming language. We started by discussing the importance of character functions and saw an example of converting a lowercase letter to uppercase using conditional statements.

The tutorial then introduces the toupper() function from the ctype.h header, which simplifies the conversion of lowercase letters to uppercase. It demonstrates the usage of toupper() in a program and explains how it handles the conversion.

Next, we learned that there are many more character handling functions available in the ctype.h header file, including several commonly used functions such as:

  • isalnum(char c): Checks if the character c is alphanumeric (an alphabet or a digit).
  • isalpha(char c): Checks if the character c is an alphabet (uppercase or lowercase).
  • isdigit(char c): Checks if the character c is a digit (0-9).
  • islower(char c): Checks if the character c is a lowercase letter (a-z).
  • isupper(char c): Checks if the character c is an uppercase letter (A-Z).
  • tolower(char c): Converts an uppercase alphabet c to lowercase.

Overall, the tutorial has provided a comprehensive introduction to character functions in C, highlighting their significance and showcasing some commonly used functions for character handling.



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.