×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to count upper case and lower case characters in a string

Problem statement

This c program will read a string and count total number of uppercase and lowercase characters in it. To test, which is lowercase or uppercase character, we need to check that character is between ‘A’ to ‘Z’ (Uppercase character) or ‘a’ to ‘z’ (Lowercase character).

Example

Input string: This is a Test String.

Output:
Total Upper case characters: 3, 
Lower Case characters: 14

C program to count upper case and lower case characters in a string

/*C program to count uppercase and lowercase 
characters in a string.*/

#include <stdio.h>

int main()
{
    char str[100];
    int countL, countU;
    int counter;

    //assign all counters to zero
    countL = countU = 0;

    printf("Enter a string: ");
    gets(str);

    for (counter = 0; str[counter] != NULL; counter++) {

        if (str[counter] >= 'A' && str[counter] <= 'Z')
            countU++;
        else if (str[counter] >= 'a' && str[counter] <= 'z')
            countL++;
    }

    printf("Total Upper case characters: %d, Lower Case characters: %d", countU, countL);

    return 0;
}

Output

Enter a string: This is a Test String.
Total Upper case characters: 3, Lower Case characters: 14

C Strings User-defined Functions Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.