Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to find total numbers vowels, constants and digits in an input file

Posted By: Alice Miller     Category: C Programming     Views: 8314

Write a Program to find total numbers vowels, constants and digits in an input file.

Code for Program to find total numbers vowels, constants and digits in an input file in C Programming

#include <stdio.h>
#include <stdlib.h>
#include "common.h"int main(int argc,char *argv[])
{
 FILE *fp;
 int id,nproc,i,*lock,*lock1,*digit,*vovel,*consonet,ldigit=0,lvovel=0,lconsonet=0,shmid[5];

 lock=(int *)shared(sizeof(int),&shmid[0]);
 sem_init(lock);
 lock1=(int *)shared(sizeof(int),&shmid[1]);
 sem_init(lock1);

 digit=(int *)shared(sizeof(int),&shmid[2]);
 *digit=0;
 vovel=(int *)shared(sizeof(int),&shmid[3]);
 *vovel=0;
 consonet=(int *)shared(sizeof(int),&shmid[4]);
 *consonet=0;

 char c='p';
     
 if(argc!=2)
 {
  printf("\nError - insufficient no of arguments.");
  getchar();
  exit(-1);
 }
 fp=fopen(argv[1],"r");
 if(fp==NULL)
 {
  printf("\nError - unable to open input file.");
  getchar();
  exit(-1);
 }
 
 printf("\nEnter total no of processes : ");
 scanf("%d",&nproc);

 id=process_fork(nproc);
 
  while(1)
  {
   sem_lock(lock);
   
     c=getc(fp);
     sem_unlock(lock);
     
       if(c==EOF)
          break; 
            
   
        if(c>='0'&&c<='9')
            ldigit++;
        elseif(c=='a'||c=='A'||c=='e'||c=='E'||c=='o'||c=='O'||c=='i'||c=='I'||
                c=='u'||c=='U')
            lvovel++;
        elseif((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
           lconsonet++;
        
    }
            
  
  sem_lock(lock1);
  *digit+=ldigit;
  *vovel+=lvovel;
  *consonet+=lconsonet;
  sem_unlock(lock1);
  process_join(id,nproc);
  printf("\nFile Name : %s",argv[1]);
  printf("\nNo of digits : %d",*digit);
  printf("\nNo of vovels : %d",*vovel);
  printf("\nNo of consonet : %d\n",*consonet);
  getchar();      
  return(0);
}
/*
Output:-

Enter total no of processes : 4

File Name : p.txt
No of digits : 4
No of vovels : 3
No of consonet : 2
*/
  
Share: 



Alice Miller
Alice Miller author of Program to find total numbers vowels, constants and digits in an input file is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!