C Programming code for counting numbers, letters in Strings

Write a modular C program to count the number of alphabets, special characters and digits in a given sentence?

CODE:

#include<stdio.h>
#include<string.h>
void read_sentence(char sentence[]);
void count(char sentence[]);
main()
{
char sentence[200];
printf("Enter the sentence:\n");
read_sentence(sentence);
count(sentence);
}
void read_sentence(char sentence[])
{
gets(sentence);
}
void count(char sentence[])
{
int alphabet_count = 0;
int digit_count = 0;
int special_count = 0;
int index = 0;
while(sentence[index]!='\0')
{
if(sentence[index] >='A' && sentence[index]<='Z')
alphabet_count++;
else if(sentence[index] >='a' && sentence[index]<='z')
alphabet_count++;
else if(sentence[index] >='0' && sentence[index]<='9')
digit_count++;
else
special_count++;
index++;
}
printf("Number of alphabets = %d\n",alphabet_count);
printf("Number of digits = %d\n",digit_count);
printf("Number of special characters = %d\n",special_count);
}

 

OUTPUT

Enter the sentence:
k*74^^ iisb& idsfb()hf 278 6%& bkvsdg^%## 9w7h38
Number of alphabets = 20
Number of digits = 10
Number of special characters = 18

Process returned 0 (0x0) execution time : 28.780 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:

Top 100+ C Programming codes – KLE Technological University

 

Leave a Comment