Logo 
Search:

C Programming Articles

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

Program to find length of a string

Posted By: Isobel Hughes     Category: C Programming     Views: 2369

Write a Program to find length of a string.

Code for Program to find length of a string in C Programming

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>

int p_fork(int);
void p_join(int,int);

int main()
{
    int sid,id,sum=0,sum0=0,nproc;
    int *ptr;
    int n,i;
    char *s;

    s=(char*)malloc(50);
    
    for(i=0;i<50;i++)
    {
        *(s+i)='\0';
    }

    printf("Enter the no. of  processes: ");
    scanf("%d",&nproc);

    printf("\nEnter any string : ");
    scanf("%s",s);
    
    sid=shmget(IPC_PRIVATE,20,IPC_CREAT|0666);

    ptr=(int*)shmat(sid,0,0);
    
    id=p_fork(nproc);
    
    *(ptr+id)=0;

     for(i=id;*(s+i)!='\0';i+=nproc)
         *(ptr+id)=*(ptr+id)+1;

    p_join(nproc,id);

    for(i=1;i<nproc;i++)
        *(ptr+0)=*(ptr+i)+*(ptr+0);

    printf("\nLength of string is : %d",*(ptr+0));
    

}

int p_fork(int x)
{
        int t;
        for(t=1;t<x;t++)
        {
                if(fork()==0)
                        return t;
        }
        return 0;
}

void p_join(int x,int id)
{
        int t;
        if(id==0)
        {
                for(t=1;t<x;t++)
                        wait(0);
        }
        else
                    exit(0);
}
  
Share: 


Didn't find what you were looking for? Find more on Program to find length of a string Or get search suggestion and latest updates.

Isobel Hughes
Isobel Hughes author of Program to find length of a string is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
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!