w3resource

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

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;
}

Sample 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:

Flowchart : Check whether an n digits number is armstrong or not

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.



Follow us on Facebook and Twitter for latest update.