Computer >> Computer tutorials >  >> Programming >> C programming

Write C program using isupper() function


Problem

How to identify a total number of upper-case alphabets in a string using C Programming?

Solution

The logic we used to count number of upper-case letters in a sentence is as follows −

for(a=string[0];a!='\0';i++){
   a=string[i];
   if (isupper(a)){
      counter=counter+1;
      //counter++;
   }
}

Example 1

#include<stdio.h>
#include<ctype.h>
void main(){
   //Declaring integer for number determination, string//
   int i=0;
   char a;
   char string[50];
   int counter=0;
   //Reading User I/p//
   printf("Enter the string :");
   gets(string);
   //Using For loop and predefined function to count upper case alpha's//
   for(a=string[0];a!='\0';i++){
      a=string[i];
      if (isupper(a)){
         counter=counter+1;
         //counter++;
      }
   }
   //Printing number of upper case alphabets//
   printf("Capital letters in string : %d\n",counter);
}

Output

Enter the string :TutoRialsPoint CPrograMMing
Capital letters in string : 7

Example 2

In this program, we shall see how to count upper case letters without using isupper() −

#include<stdio.h>
int main(){
   int upper = 0;
   char string[50];
   int i;
   printf("enter The String : \n");
   gets(string);
   i = 0;
   while(string[i]!= ' '){
      if (string[i] >= 'A' && string[i] <= 'Z')
         upper++;
         i++;
   }
   printf("\nUppercase Letters : %d", upper);
   return (0);
}

Output

enter The String :
TutOrial
Uppercase Letters : 2