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 :
- Ask the user to enter a string. Define four variables to store the count of uppercase character,lowercase character,special character, and number.
- Run one for loop to read all characters of the string one by one.
- Check for each character. Check if it an uppercase character,lowercase character,special character or number. Increment the variable holding the count as well.
- 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:
- 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.
- Ask the user to enter a string and save it in the inputString variable.
- Print out the user input string.
- Set the values of all integer values to zero.
- Run one for loop to scan all characters of the string one by one.
- Check for each character. If it is uppercase,lowercase,number or special character.Increment the values of the flags accordingly.
- 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:
- C program to find out the palindrome number in a range
- C program to count and print frequency of each letter in a word
- C program to calculate simple interest
- C program to insert an element in an array at any specific position
- C program to check if a number is in a range with one line
- C program to print two arrays using a separate function
- Swap two numbers without using a third number using a macro in C