C Exercises: Count the upper-case, lower-case and other characters
C For Loop: Exercise-61 with Solution
Write a C program that takes input from the user and counts the number of uppercase and lowercase letters, as well as the number of other characters.
Note: To exit On Linux systems and OS X, the character to input to cause an EOF is CTRL+D. For Windows, it's CTRL+Z
This C program prompts the user to input a string. It then iterates through each character of the input string, counting the number of uppercase letters, lowercase letters, and other characters (such as digits, symbols, or whitespace). Finally, it prints out the counts of uppercase letters, lowercase letters, and other characters.
Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
#include <ctype.h>
int main(void)
{
// Variable declarations and initialization
int chr;
int uppercase_ctr = 0, lowercase_ctr = 0, other_ctr = 0;
// Prompting the user for input
printf("\nInput characters: On Linux systems and OS X EOF is CTRL+D. For Windows EOF is CTRL+Z.\n");
// Loop to process characters until EOF (End of File) is encountered
while ((chr = getchar()) != EOF)
{
// Checking if the character is uppercase
if (isupper(chr))
uppercase_ctr++;
// Checking if the character is lowercase
else if (islower(chr))
lowercase_ctr++;
// If it's not uppercase or lowercase, consider it as another character
else
other_ctr++;
}
// Printing the counts of different types of characters
printf("\nUppercase letters: %d\n", uppercase_ctr);
printf("Lowercase letters: %d\n", lowercase_ctr);
printf("Other characters: %d\n", other_ctr);
return 0;
}
Output:
Input characters: On Linux systems and OS X EOF is CTRL+D. For Windows EOF is CTRL+Z. Uppercase letters: 0 Lowercase letters: 9 Other characters: 1
Flowchart:
C Programming Code Editor:
Previous: Counts the number of characters left in the file.
Next: C Searching and Sorting Algorithm Exercises Home
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming-exercises/for-loop/c-for-loop-exercises-61.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics