C Program to Count Characters, Digits and Alphabets

Write a C program to count characters, digits and alphabets till user enters EOF on thw command line. This C program continues to a stream of characters until user enters EOF (Ctrl + D on Linux) on command line and then finds how many cuxracters, digits and alphabets are accepted.

C program to count words, characters, and lines or sentences from command line

/* Aim: Write a program to accept characters till the user enters EOF(^D on Linux and ^Z on windows)
and count number of alphabets and digits entered. */

#include<stdio.h>
int main()
{ 
	char ch;
	int count=0,alpha=0,digit=0;

		while((ch=getchar())!=EOF)
		{
		if (ch>=65 && ch<=123)
		{
		++alpha;
		}
		if (ch>=48 && ch<=58)
		{
		++digit;
		}
		++count;
		if(ch==10 || ch==13)
		{
		--count; 
		}
	} 

	printf("\n %d characters are written \n \n",count);
	printf(" %d  digits are written \n \n",digit);
	printf(" %d alphabets are written \n \n",alpha);

	return 0;
}

Output:

  123456789
  abcdefg

 16 characters are written 
 
 9  digits are written 
 
 7 alphabets are written

Comments