C Program to Extract String from a String

C Program to Extract String from a String .Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int subString(char [],char [],int ,int );
main()
{
      char text[50],target[50];
      int start,end,isError;
      
      printf("\nProgram to Extract String from a String \n");
      printf("\n----------------------------------------------\n\n\n");
      printf("Enter Text : ");                                                   
      gets(text);
      
     
      
      printf("\nStart Position : ");
      scanf("%d",&start);
      
      printf("\n\nINFO : Enter -1 if you don't want end position \n\n");
      
      printf("\nEnd Position : ");
      scanf("%d",&end);
      
      isError=subString(text,target,start,end);
      
      if(isError==0)
      printf("\nERROR !!! End position can not be less than Start position ");
      else
      printf("\nExtracted String is : %s",target);
      
      getch();
      
}

int subString(char source[],char target[],int start,int end)
{
     char *p;
     int i,k=0;
     
     if((end<start)&&(end!=-1))
     return 0;
     
     if(end==-1)
     end=strlen(source);
     
                
     for(i=start;i<=end;i++)
     {
                    p=&source[i];
                    target[k]=*p;
                    k++;         
     }
     
     target[k]='\0';
     
}

Output

substring

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s