About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Wednesday, September 4, 2013

C Program to Count number of Uppercase and Lowercase Letters

#include<stdio.h>
#include<conio.h>
void main()
{
int upper=0,lower=0;
char ch[80];
int i;
clrscr();
printf("\nEnter The String : ");
gets(ch);

i=0;
while(ch[i]!='')
  {
  if(ch[i]>='A' && ch[i]<='Z')
     upper++;
  if(ch[i]>='a' && ch[i]<='z')
     lower++;
  i++;
  }
 printf("\nUppercase Letters : %d",upper);
 printf("\nLowercase Letters : %d",lower);

 getch();
 }

Output :
Enter The String : Pritesh A Taral
Uppercase Letters : 3
Lowercase Letters : 10



Explanation :
if(ch[i]>='A' && ch[i]< ='Z')
  • We can compare two Characters.
  • Generally Comparing Characters means Comparing Corresponding ASCII Values.
if('Z' > 'A')
means
if('Z' > 'A')
 = if(ASCII Value 'Z' > ASCII Value 'A')
 = if(90 > 65)

1 comment:

zzzzz said...

Thanks.. Here is a similar program to convert Uppercase to lowercase and
lowercase to uppercase