Character Processing (<ctype.h>)
The <ctype.h> header file declares several functions for testing characters. For each function, the argument is an int whose value must be EOF or representable as an unsigned char , and the return value is an integer.
Functions
int isalnum(int c);
Returns a nonzero integer if the character passed to it is an alphanumeric ASCII character. Otherwise, isalnum returns 0.
int isalpha(int c);
Returns a nonzero integer if the character passed to it is an alphabetic ASCII character. Otherwise, isalpha returns 0.
int iscntrl(int c);
Returns a nonzero integer if the character passed to it is an ASCII DEL character (177 octal, 0x7F hex) or any nonprinting ASCII character (a code less than 40 octal, 0x20 hex). Otherwise, iscntrl returns 0.
int isdigit(int c);
Returns a nonzero integer if the character passed to it is a decimal digit character (0 to 9). Otherwise, isdigit returns 0.
int isgraph(int c);
Returns a nonzero integer if the character passed to it is a graphic ASCII character (any printing character except a space character). Otherwise, isgraph returns 0.
int islower(int c);
Returns a nonzero integer if the character passed to it is a lowercase alphabetic ASCII character. Otherwise, islower returns 0.
int isprint(int c);
Returns a nonzero integer if the character passed to it is an ASCII printing character, including a space character. Otherwise, isprint returns 0.
int ispunct(int c);
Returns a nonzero integer if the character passed to it is an ASCII punctuation character (any printing character that is nonalphanumeric and greater than 40 octal, 0x20 hex). Otherwise, ispunct returns 0.
int isspace(int c);
Returns a nonzero integer if the character passed to it is white space. Otherwise, isspace returns 0. The standard white space characters are:
space (' ')
form feed ('\f')
new line ('\n')
carriage return ('\r')
horizontal tab ('\t')
vertical tab ('\v')
Returns a nonzero integer if the character passed to it is white space. Otherwise, isspace returns 0. The standard white space characters are:
space (' ')
form feed ('\f')
new line ('\n')
carriage return ('\r')
horizontal tab ('\t')
vertical tab ('\v')
int isupper(int c);
Returns a nonzero integer if the character passed to it is an uppercase alphabetic ASCII character. Otherwise, isupper returns 0.
int isxdigit(int c);
Returns a nonzero integer if the character passed to it is a hexadecimal digit (0 to 9, A to F, or a to f). Otherwise, isxdigit returns 0.
int tolower(int c);
Converts an uppercase letter to lowercase. c remains unchanged if it is not an uppercase letter.
int toupper(int c);
Converts a lowercase letter to uppercase. c remains unchanged if it is not a lowercase letter.
Example:
The following program will count number of alphabets, digits and spaces in a string
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[100];
int i,dc=0,al=0,sp=0;
printf("Enter a string \n");
fgets(str,100,stdin);
for(i=0;i<strlen(str);i++)
{
if(isalpha(str[i])) al++;
if(isdigit(str[i])) dc++;
if(isspace(str[i])) sp++;
}
printf("Number of digits=%d\n",dc);
printf("Number of alphabets=%d\n",al);
printf("Number of spaces=%d\n",sp);
}
#include <string.h>
int main(void)
{
char str[100];
int i,dc=0,al=0,sp=0;
printf("Enter a string \n");
fgets(str,100,stdin);
for(i=0;i<strlen(str);i++)
{
if(isalpha(str[i])) al++;
if(isdigit(str[i])) dc++;
if(isspace(str[i])) sp++;
}
printf("Number of digits=%d\n",dc);
printf("Number of alphabets=%d\n",al);
printf("Number of spaces=%d\n",sp);
}
Output
Enter a string
this is test123
Number of digits=3
Number of alphabets=10
Number of spaces=3
this is test123
Number of digits=3
Number of alphabets=10
Number of spaces=3
Write a C program to read an English Alphabet through keyboard and display whetherthe given Alphabet is in upper case or lower case.
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
printf("Enter a character\n");
ch=getchar();
if(isupper(ch))
printf("uppercase\n");
else
printf("lower case\n");
}
Develop a C program to accept a string from the user. Display the count of upper case and lowercase characters in that string. ( university question)
#include <stdio.h>
int main() {
char str[100];
int uppercase_count = 0, lowercase_count = 0;
int i = 0;
// Input string from the user
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Count uppercase and lowercase characters
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z') {
uppercase_count++;
} else if (str[i] >= 'a' && str[i] <= 'z') {
lowercase_count++;
}
i++;
}
// Display counts
printf("Number of uppercase characters: %d\n", uppercase_count);
printf("Number of lowercase characters: %d\n", lowercase_count);
return 0;
}
Note: we can also use builtin functions
Comments
Post a Comment