- Printable Characters: The characters that are shown on the terminal are known as printable characters.
- Control Characters: Those who are ordered to carry out a particular action.
Character functions should exclusively receive integer type parameters. In cases where characters are provided as arguments instead of integers, they are converted into integers using the corresponding ASCII values.
For standard characters, the functions detailed below are employed within the header file <ctype.h/cctype>. When dealing with wide characters, functions operating on type wchar_t are used.
| S no. | Function | Description | Return value |
|---|---|---|---|
1. |
isalnum() | With this feature, alphanumeric characters are recognized. | If the supplied parameter is a non-alphanumeric character, it returns 0.if the supplied argument is an alphanumeric character, returns a value that is not zero. |
2. |
isalpha() | The alphabets are distinguished from other characters by this function. | If the supplied parameter is not an alphabet, it returns 0.If the supplied input is an alphabet, it returns a non-zero result. |
3. |
isblank() | This method distinguishes between blank spaces and other characters. | If the supplied input is not a blank space, it returns 0.returns a value other than zero if the supplied argument is a space. |
4. |
iscntrl() | These control characters are recognised by this function: \n, \b, \t, and \r. | If the supplied argument is not a control character, it returns 0.if the passed argument is a control character, it returns a value other than zero. |
5. |
isdigit() | This function recognises character-based numbers. | If the passed input is not an integer, it returns 0.if a number is the provided parameter, returns a value that is not zero. |
6. |
islower() | Lowercase alphabet identification is done by this function. | If the supplied argument is not a lowercase alphabet, it returns 0.if the supplied parameter is a lowercase alphabet, it returns a value other than zero. |
7. |
isprint() | The printable characters are identified by this function. | If the supplied argument is an unprintable character, it returns 0.if the supplied argument is a printable character, it returns a value other than zero. |
8. |
ispunct() | Punctuation characters are identified by this function (characters that are neither alphanumeric nor space). | If the supplied argument is not a punctuation character, it returns 0.If the passed argument is a punctuation character, it returns a value other than zero. |
9. |
isspace() | White-space characters are identified using this function. | If the supplied argument is not a white-space character, it returns 0.If the passed parameter is a white-space character, it returns a value other than zero. |
10. |
isupper() | The uppercase alphabets are recognised by this function. | If the supplied parameter is not an uppercase alphabet, it returns 0.If the supplied parameter is an uppercase alphabet, a nonzero result is returned. |
11. |
isxdigit() | The hexadecimal digit is identified using this function. | If the supplied parameter is not a hexadecimal digit, it returns 0.If the supplied parameter is a hexadecimal digit, it returns a value other than zero. |
12. |
tolower() | This function changes the alphabet from uppercase to lowercase. | returns the relevant uppercase alphabet's lowercase equivalent. |
13. |
toupper() | This function changes the alphabet from lowercase to uppercase. | Returns the appropriate lowercase alphabet's uppercase counterpart. |
Here are some examples of how to use some of the aforementioned functions:
Example 1: The program provided below counts the letters and numbers:
#include <stdio.h>
// Header file containing character functions
#include <ctype.h>
void identify_alpha_numeric(char a[])
{
int count_alpha = 0, count_digit = 0;
for (int i = 0; a[i] != '\0'; i++) {
// To check the character is alphabet
if (isalpha(a[i]))
count_alpha++;
// To check the character is a digit
if (isdigit(a[i]))
count_digit++;
}
printf("The number of alphabets are %d\n",
count_alpha);
printf("The number of digits are %d",
count_digit);
}
int main()
{
// String Initialization
char a[]
= "Hi 123455, "
" Logic Practice is what you need";
identify_alpha_numeric(a);
}
Output
The number of alphabets are 23
The number of digits are 6
………………………………………………..
Process executed in 1.11 seconds
Press any key to continue.
Explanation
In the previously shown C program example, we have illustrated the application of the isalpha and isdigit functions to tally the quantity of letters and numbers.
The software presented here tallies the uppercase and lowercase letters in the alphabet and alters their letter case.
#include <stdio.h>
// Header file containing character functions
#include <ctype.h>
char* identify_convert_ul(char a[])
{
int count_upper = 0, count_lower = 0;
for (int i = 0; a[i] != '\0'; i++) {
// To check the uppercase characters
if (isupper(a[i])) {
count_upper++;
a[i] = tolower(a[i]);
}
// To check the lowercase characters
else if (islower(a[i])) {
count_lower++;
a[i] = toupper(a[i]);
}
}
printf("No. of uppercase characters are %d\n",
count_upper);
printf("No. of lowercase characters are %d",
count_lower);
return a;
}
int main()
{
// String Initialization
char a[] = "Hello, Welcome to Logic Practice";
char* p;
p = identify_convert_ul(a);
printf("%s", p);
}
Output
No. of uppercase alphabets are 3
No. of lowercase alphabets are 21
hEELLO, wELCOME TO JAVALOGIC PRACTICE
...........................................................
Process executed in 1.11 seconds
Press any key to continue
Explanation
In the preceding C program example, we showcased the utilization of the functions isupper and islower. These functions are employed to determine whether alphabetic characters are uppercase or lowercase, and can convert their case accordingly.
Example 3: This particular software generates a line break for every word it displays.
#include <stdio.h>
// Header file containing character functions
#include <ctype.h>
char* print_word(char a[])
{
for (int i = 0; a[i] != '\0'; i++) {
// Space is replaced
// with control character '\n'
if (isblank(a[i]))
a[i] = '\n';
}
return a;
}
int main()
{
// String Initialization
char a[] = "Hi Everyone."
" Welcome to Logic Practice.com";
char* p;
p = print_word(a);
printf("%s", p);
}
Output
Hi
Everyone.
Welcome
to
javaLogic Practice.com
............................
Process executed in 1.11 seconds
Press any key to continue.
Explanation
In the provided C program example, we have showcased the functionality of the isblank function, which inserts a newline for every word it displays.