C - Single Character Library Functions
In C, there are several single-character library functions available for character manipulation. These functions allow you to perform various operations on individual characters. Here are some common single-character library functions along with illustrations for each one:
1. isalpha
- Checks if a character is an alphabetic character (a letter):
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
if (isalpha(ch)) {
printf("%c is an alphabetic character.\n", ch);
} else {
printf("%c is not an alphabetic character.\n", ch);
}
return 0;
}
2. isdigit
- Checks if a character is a digit (0-9):
#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;
}
3. isalnum
- Checks if a character is alphanumeric (a letter or a digit):
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'X';
if (isalnum(ch)) {
printf("%c is alphanumeric.\n", ch);
} else {
printf("%c is not alphanumeric.\n", ch);
}
return 0;
}
4. islower
- Checks if a character is a lowercase letter:
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'z';
if (islower(ch)) {
printf("%c is a lowercase letter.\n", ch);
} else {
printf("%c is not a lowercase letter.\n", ch);
}
return 0;
}
5. isupper
- Checks if a character is an uppercase letter:
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'Q';
if (isupper(ch)) {
printf("%c is an uppercase letter.\n", ch);
} else {
printf("%c is not an uppercase letter.\n", ch);
}
return 0;
}
6. tolower
- Converts an uppercase character to lowercase:
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
char lowercase = tolower(ch);
printf("%c converted to lowercase is %c.\n", ch, lowercase);
return 0;
}
7. toupper
- Converts a lowercase character to uppercase:
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'z';
char uppercase = toupper(ch);
printf("%c converted to uppercase is %c.\n", ch, uppercase);
return 0;
}
These functions are part of the ctype.h
library and are useful for character classification and conversion operations in C programs.