C program to find total lowercase,uppercase,digits etc. in a string

C program to find out the count of lowercase,uppercase,special character and digit in a string :

In this C programming tutorial, we will learn how to find the count of lowercase characters,uppercase characters,special characters and numeric characters in a string. The program will ask the user to enter one string, it will find out the count, and then print out the result. The algorithm we are using is as below :

Algorithm :

  1. Ask the user to enter a string. Define four variables to store the count of uppercase character,lowercase character,special character, and number.
  2. Run one for loop to read all characters of the string one by one.
  3. Check for each character. Check if it an uppercase character,lowercase character,special character or number. Increment the variable holding the count as well.
  4. Finally, print out the count for each.

Now, let’s take a look at the C program to implement this algorithm :

C program :

#include <stdio.h>

int main()
{
    //1
    char inputString[100];
    int upperCount, lowerCount, specialCount, digitCount, i;

    //2
    printf("Enter a String : ");
    gets(inputString);

    //3
    printf("String input is %s ", inputString);

    //4
    upperCount = lowerCount = specialCount = digitCount = 0;

    //5
    for (i = 0; inputString[i] != '\0'; i++)
    {
        //6
        if (inputString[i] >= 'A' && inputString[i] <= 'Z')
        {
            upperCount++;
        }
        else if (inputString[i] >= 'a' && inputString[i] <= 'z')
        {
            lowerCount++;
        }
        else if (inputString[i] >= '0' && inputString[i] <= '9')
        {
            digitCount++;
        }
        else
        {
            specialCount++;
        }
    }

    //7
    printf("\nUpper case count : %d \n", upperCount);
    printf("Lower case count : %d \n", lowerCount);
    printf("Digit count : %d \n", digitCount);
    printf("Special character count : %d \n", specialCount);

    return 0;
}

Explanation :

The commented numbers in the above program denote the step number below:

  1. Create one character array inputString to hold the input string. Create four integers to store the count of uppercase characters,lowercase characters,special characters and integers.
  2. Ask the user to enter a string and save it in the inputString variable.
  3. Print out the user input string.
  4. Set the values of all integer values to zero.
  5. Run one for loop to scan all characters of the string one by one.
  6. Check for each character. If it is uppercase,lowercase,number or special character.Increment the values of the flags accordingly.
  7. After the loop is completed, print out the values of each flag.

Sample Output :

Enter a String : Hello world 112@#$
String input is Hello world 112@#$
Upper case count : 1
Lower case count : 9
Digit count : 3
Special character count : 5

Enter a String : Sample112@#$
String input is Sample112@#$
Upper case count : 1
Lower case count : 5
Digit count : 3
Special character count : 3

You might also like: