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.
Before moving further, we’ve prepared a video tutorial to understand what Char Functions are in C:
Video: Char Functions in C
Video: Char Functions in C (Hindi)
For example, you can check if a character is a digit, a letter, a space, a punctuation mark, etc.
ctype.h
Include it in your program file:
#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:
// 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
W is an alphabetic letter.
isdigit() function in C
The isdigit() checks if the character is a digit. Let us see an example:
// 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
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:
// 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
' ' is a whitespace character.
toupper() function in C
The toupper() converts a character to uppercase. Let us see an example:
// 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
Uppercase of t is T
tolower() function in C
The tolower() converts a character to uppercase. Let us see an example:
// 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
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