16 Jan Char Functions in C
C provides several standard library functions to work with characters. These functions help in handling and classifying characters in terms of their types. For example, you can check if a character is a digit, a letter, a space, a punctuation mark, etc.
1 2 3 |
ctype.h |
Include it in your program file:
1 2 3 |
#include <ctype.h> |
Let us now see some popular character functions in C language:
- isalpha() checks if the character is an alphabetic letter
- isdigit() checks if the character is a digit.
- isspace() checks if the character is a whitespace character, space, tab, new line, etc.
- toupper() converts a character to uppercase.
- tolower() converts a character to lowercase.
Let us see an example implementing all the above character functions:
isalpha() function in C
The isalpha() checks if the character is an alphabetic letter. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// isalpha() function in C #include<stdio.h> #include<ctype.h> int main() { char ch = 'W'; // Check if ch is an alphabetic letter if(isalpha(ch)) { printf("%c is an alphabetic letter.", ch); } else { printf("%c is not an alphabetic letter.", ch); } return 0; } |
Output
1 2 3 |
W is an alphabetic letter. |
isdigit() function in C
The isdigit() checks if the character is a digit. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// isdigit() function in C #include<stdio.h> #include<ctype.h> int main() { char ch = '9'; // Check if ch is a digit if(isdigit(ch)) { printf("%c is a digit.", ch); } else { printf("%c is not a digit.", ch); } return 0; } |
Output
1 2 3 |
9 is a digit. |
isspace() function in C
The isspace() checks if the character is a whitespace character, space, tab, new line, etc. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// isspace() function in C #include<stdio.h> #include<ctype.h> int main() { char ch = ' '; // Check if ch is a whitespace if(isspace(ch)) { printf("'%c' is a whitespace character.", ch); } else { printf("%c is not a whitespace character.", ch); } return 0; } |
Output
1 2 3 |
' ' is a whitespace character. |
toupper() function in C
The toupper() converts a character to uppercase. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// toupper() function in C #include<stdio.h> #include<ctype.h> int main() { char ch = 't'; // Convert to uppercase printf("Uppercase of %c is %c", ch, toupper(ch)); return 0; } |
Output
1 2 3 |
Uppercase of t is T |
tolower() function in C
The tolower() converts a character to uppercase. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// tolower() function in C #include<stdio.h> #include<ctype.h> int main() { char ch = 'Y'; // Convert to lowercase printf("Lowercase of %c is %c", ch, tolower(ch)); return 0; } |
Output
1 2 3 |
Lowercase of Y is y |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments