Friday 26 October 2012

Count no. of words and vowels

/*
Program where parent process Counts number of vowels in the given sentence
 And child process will count number of words in the same sentence
*/

#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>

parent_p(char str[])
{
    int j,a=0,e=0,i=0,o=0,u=0;
    for(j=0;str[j];j++)
    {
        switch(str[j])
        {
            case 'a':
            case 'A':a++;break;
            case 'e':
            case 'E':e++;break;
            case 'i':
            case 'I':i++;break;
            case 'o':
            case 'O':o++;break;
            case 'u':
            case 'U':u++;break;
        }
    }
    printf("\nNumber of a or A = %d",a);
    printf("\nNumber of e or E = %d",e);
    printf("\nNumber of i or I = %d",i);
    printf("\nNumber of o or O = %d",o);
    printf("\nNumber of u or U = %d\n",u);   
}
       
child_p(char str[])
{
    int words=0,i=0,flag=0;
    while(str[i]!='\0')
    {
        while(str[i]==' ' && str[i]!='\0')
        {
            i++;
        }
        while(str[i]!=' ' && str[i]!='\0')
        {
            i++;
            flag=1;
        }
        if(flag)
        {
            words++;
            flag=0;
        }
    }
    printf("\nNumber of words = %d\n",words);
}

int main()
{
    pid_t pid;
    char str[50];
    printf("\nEnter the string\t");
    gets(str);
    pid = fork();
    if(pid<0)
    {
        printf("\nError in process creation");
        exit(-1);
    }
    else if(pid==0)
    {
        printf("\nChild ID = %d",getpid());
        child_p(str);
        exit(0);
    }
    else
    {
        printf("\nParent ID = %d",getppid());
        execlp("a.out","Fork_Vowel.c",parent_p(str),NULL);
        sleep(10);
        wait();
       
        exit(0);
    }
    return 0;
}


       

No comments:

Post a Comment