Monday, April 30, 2012

C Program That Delete A Substring From The Main String


Hello friends,today i share a program which i made in 4hours but with my own hardwork,i am sure that you can easily understand this program because i make this program only for my friends,please read it carefully try to understand it.



#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 char str[100],sub[100];
 int i,j,k,l,f=0,g=0,m,h,v;
 clrscr();
 printf("\n Enter the string:");
 gets(str);


 printf("\n Enter the substring:");
 gets(sub);

 l=strlen(sub);
 m=strlen(str);

 printf("%d %d",l,m);
 for(i=0;str[i]!='\0';i++)
 {
  k=i;
  j=0;
  while(sub[j]!='\0')
  {
   if(str[k]==sub[j])
   {
    g++;
    if(g==l)
    {
     f=1;
     h=i;
    }

   }
   else if(str[k]!=sub[j])
   {
    break;
   }
   j++;
   k++;
  }

 }
 if(f==1)
 {
  printf("\n sub string is found\n\n\n");
  for(i=0;i<m;i++)
  {
   if(i<h)
    printf("%c",str[i]);
   else if(i>h&&i<(h+l))
    continue;
   else if(i>(h+l))
    printf("%c",str[i]);
  }
 }
 else if(f==0)
 {
  printf("\n substring not found:");
 }
 getch();
 return(0);
}



      Thanking You Guys........

7 comments:

  1. nice, but you are just printing the string leaving the substring present in it, how can you change the main string itself by deleting the substring from it? i am guessing by using a[pos] = a[pos+1] as many times as the substring length starting from the first position of the substring in the string, what do you think?

    ReplyDelete
  2. But why doing a program with so many variables.

    ReplyDelete
  3. Your code is not too perfect there is lots of bug in your code .
    You are not deleting the multiple occurrence and statements like souravis he is a boy will give a output like sourav he is a boy that is not correct..

    ReplyDelete

Please Give Me Your Views

Popular Posts