Showing posts with label LowerCase. Show all posts
Showing posts with label LowerCase. Show all posts

C program to count the number of lower upper case letters and digits in a given file named data.txt

 In this I tell how to write a C program that count number of letters and digits from a given file. C program to display text from file on output. So, without wasting our time let's get started .

Code:

#include<stdio.h>
#include<stdlib.h>

int main(){
  FILE *ptr;//this we make to point towards file 
  ptr=fopen("data.txt","r");//we are opening the file  in reading mode 
  char ch;//to store char variable 
  int int_count=0;      //we need to initilize counter from zero 
  int letter_count=0;   //we need to initilize counter from zero 
  while (1)
  {
  ch=fgetc(ptr);
  if((ch>=65 && ch<=90) || (ch>=97 && ch<=122)){
      letter_count++;
  }
  else if(ch>=48 && ch<=57){
      int_count++;
  }
  else if(ch==EOF){
      break;
  }
  }
  printf("Number of letters :  %d\n",letter_count);
  printf("Number of Integers :  %d\n",int_count);
  fclose(ptr);//this will close file  


    return 0;
}

Screenshot of Text file:

                      Here we write alphabets and Integers in Text file

Logic:

for our program we need to create a text file . Here I create file name data.txt. Here we first make pointer which points towards file ptr.  And a variable ch of type character so that it can get character from file in it .And we also declare two variable int_count and letter_count and initialize both form 0 And then we open a file using Fopen  in reading mode . And then we run a infinite loop and control only come out form it only when ch is equal to EOF(end of file) and break take him out of loop  . And we give condition for it in last else if. Now let's talk about other two if condition . In first if we check whether ch comes between or equal(65 to 90) is Ascii value for small alphabet .Then we use || (or) and give Ascii value for big alphabet ( 97 to 122). And if condition comes true then increase value of letter_cout  by one . And if not then it comes in else if and we check ch  for Integers using their Ascii value(48 to 57). And this condition is true then increase value of int_count by one. Then we print using printf number of letters and number of integers . Then we release our buffer that is reversed by file using fclose.

Output:


  Here you can see the output that it give number of letters 40 and number of integers 6 . And we get our desired output . Try this program by self and please provide me feedback by commenting your views. I hope you find this helpful  in your  work . 
                                                   Thanks  for reading



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.