Write a C program to count the number of uppercase and lowercase characters in a string.
#include <stdio.h>
#include <string.h>
int main() {
char string[256];
int upper, lower, i;
upper = lower = i = 0;
/* get the input strin from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* count the number of upper and lower case characters */
while (string[i] != '\0') {
/* number of upper case characters */
if (string[i] >= 'A' && string[i] <= 'Z') {
upper++;
} else if (string[i] >= 'a' && string[i] <= 'z') {
/* no of lowercase characters */
lower++;
}
i++;
}
/* print the result */
printf("Number of uppercase characters: %d\n", upper);
printf("Number of lowercase characters: %d\n", lower);
return 0;
}
#include <string.h>
int main() {
char string[256];
int upper, lower, i;
upper = lower = i = 0;
/* get the input strin from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* count the number of upper and lower case characters */
while (string[i] != '\0') {
/* number of upper case characters */
if (string[i] >= 'A' && string[i] <= 'Z') {
upper++;
} else if (string[i] >= 'a' && string[i] <= 'z') {
/* no of lowercase characters */
lower++;
}
i++;
}
/* print the result */
printf("Number of uppercase characters: %d\n", upper);
printf("Number of lowercase characters: %d\n", lower);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string:UPPERCASE lowercase
Number of uppercase characters: 9
Number of lowercase characters: 9
Enter your input string:UPPERCASE lowercase
Number of uppercase characters: 9
Number of lowercase characters: 9
No comments:
Post a Comment