Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What are character analysis function, explain with C program?
Character analysis functions in C are predefined functions in the ctype.h library used for analyzing and categorizing character input. These functions help determine the type of character (alphabet, digit, space, etc.) and convert between uppercase and lowercase letters.
Syntax
#include <ctype.h> int isalpha(int c); int isdigit(int c); int isupper(int c); int islower(int c); int toupper(int c); int tolower(int c);
Character Analysis Functions
| S.No | Function | Description |
|---|---|---|
| 1 | isalpha() | Checks if character is an alphabet (a-z, A-Z) |
| 2 | isdigit() | Checks if character is a digit (0-9) |
| 3 | isspace() | Checks if character is whitespace (space, tab, newline) |
| 4 | ispunct() | Checks if character is a punctuation mark |
| 5 | islower() | Checks if character is lowercase letter |
| 6 | isupper() | Checks if character is uppercase letter |
| 7 | isalnum() | Checks if character is alphanumeric (letter or digit) |
Character Conversion Functions
| Function | Description |
|---|---|
| tolower() | Converts uppercase letter to lowercase |
| toupper() | Converts lowercase letter to uppercase |
Example: Character Analysis and Conversion
Here's a comprehensive program demonstrating various character analysis and conversion functions −
#include <stdio.h>
#include <ctype.h>
int main() {
char ch1 = 'A';
char ch2 = '7';
char ch3 = '@';
char ch4 = ' ';
char ch5 = 'z';
printf("Character Analysis:<br>");
printf("Character '%c': ", ch1);
if (isalpha(ch1)) printf("alphabet ");
if (isupper(ch1)) printf("uppercase ");
printf("<br>");
printf("Character '%c': ", ch2);
if (isdigit(ch2)) printf("digit ");
if (isalnum(ch2)) printf("alphanumeric ");
printf("<br>");
printf("Character '%c': ", ch3);
if (ispunct(ch3)) printf("punctuation ");
printf("<br>");
printf("Character '%c': ", ch4);
if (isspace(ch4)) printf("whitespace ");
printf("<br>");
printf("\nCharacter Conversion:<br>");
printf("'%c' to uppercase: '%c'<br>", ch5, toupper(ch5));
printf("'%c' to lowercase: '%c'<br>", ch1, tolower(ch1));
return 0;
}
Character Analysis: Character 'A': alphabet uppercase Character '7': digit alphanumeric Character '@': punctuation Character ' ': whitespace Character Conversion: 'z' to uppercase: 'Z' 'A' to lowercase: 'a'
Example: String Character Analysis
This example analyzes each character in a string −
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char str[] = "Hello123!";
int alpha = 0, digit = 0, punct = 0;
printf("String: %s<br>", str);
printf("Character breakdown:<br>");
for (int i = 0; i < strlen(str); i++) {
printf("'%c' - ", str[i]);
if (isalpha(str[i])) {
printf("alphabet");
alpha++;
} else if (isdigit(str[i])) {
printf("digit");
digit++;
} else if (ispunct(str[i])) {
printf("punctuation");
punct++;
}
printf("<br>");
}
printf("\nSummary: %d alphabets, %d digits, %d punctuation marks<br>",
alpha, digit, punct);
return 0;
}
String: Hello123! Character breakdown: 'H' - alphabet 'e' - alphabet 'l' - alphabet 'l' - alphabet 'o' - alphabet '1' - digit '2' - digit '3' - digit '!' - punctuation Summary: 5 alphabets, 3 digits, 1 punctuation marks
Key Points
- All
ctype.hfunctions return non-zero (true) if condition is met, zero (false) otherwise. - Conversion functions return the converted character, or original character if no conversion is possible.
- These functions work with single characters, not strings directly.
Conclusion
Character analysis functions from ctype.h provide efficient ways to classify and convert characters in C. They are essential for text processing, input validation, and string manipulation tasks.
Advertisements
