C program to count number of lowercase and Upper case letters in String

 In this post I will tell you how to make a program that count number of uppercase letters and lowercase letters in a string  Know about counting integers . So, without any further delay let's started.

Code:

#include<stdio.h>
#include<string.h>

void numberLetter(char s[]){
    int i=0;
    int j=0;
    int k=0;
    while(i<strlen(s)){
      if(s[i]>=65 && s[i]<=90){
             j++;
      }
      else if(s[i]>=97 && s[i]<=122){
          k++;
      }
      i++;
    }
    printf("Number of Upper case letters : %d\n",j);
    printf("Number of Lower case letters : %d\n",k);
}
int main(){
char str[100];
printf("Enter any String \n");
scanf(%[^\n]s",str);//take the input till user press enter 
numberLetter(str);
    return 0;
}

Logic:

Here first we declare a String of name str[100] that will take up to hundred characters. Then we use printf statement to said user to enter string . And using scanf we take input . And you see I give some space between  double quote and % because we are telling compiler that only read string not white space.  Here we ^\n use this operator to tell compiler take input till user press Enter. Then we define a function called numberLetter which take string as its arguments. And in that we define three variable of int i , j ,k and initialize it from 0 . Then we run a while loop till the length given of string and we gets its length using strlen() function and that is why we include string.h our header file .  Then we use if condition and check each character of string between  and equal to 65 and 90 . Because they are their Ascii value . If this condition is true then j++  increase value of j. And for lower case we check between and equal to 97 and 122. And if this condition is true then k++ will increase value of k. Then we give i++ which will get in work till our loop condition. And value of j and every time when condition is true.  And in end we use printf to print the output . Know about basic C. Then we call numberLetter function in main and give str as parameter. 

Output:


You see that it works very well as we think according to logic and code. You must try this by yourself and become better programmer . I hope that you find this helpful in your work .                                     Thanks for reading.


No comments:

Post a Comment