#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main ()
{
char a[50],b[50];
int i,j,temp,p[10]={0},ch,x,pos;
printf("Enter the string\t");
gets(a);
printf("substring\t");
gets(b);
printf("\n1.search\n2.delete\n\n\t enter your choice\t");
scanf("%d",&ch);
for(i=0,x=0;i<strlen(a);i++)
{
j=0;
temp=0;
if(a[i]==b[0])
{
pos=i;
while(a[i]==b[j])
{
temp=1;
i++;
j++;
}
}
if((j>=strlen(b))&&(temp==1))
{
p[x]=pos+1;
x++;
}
}
if(ch==1)
{
if(p[0]==0)
printf("\nsubstring not found");
else
{
printf("\nThe substring is at the position:\t");
for(i=0;i<x;i++)
printf("%d\t",p[i]);
}
printf("\n");
exit(0);
}
else if(ch==2)
{
if(p[0]!=0)
{
printf("\n string after the deletion\t");
pos=0;
for(x=0;p[x]!=0;x++)
{
for(i=pos;i<p[x]-1;i++)
printf("%c",a[i]);
pos=p[x]-1+strlen(b);
}
for(i=pos;a[i]!='\0';i++)
printf("%c",a[i]);
}
else
printf("deletion is not possible");
}
else
printf("invalid choice");
printf("\n");
}
#include<string.h>
#include<stdlib.h>
main ()
{
char a[50],b[50];
int i,j,temp,p[10]={0},ch,x,pos;
printf("Enter the string\t");
gets(a);
printf("substring\t");
gets(b);
printf("\n1.search\n2.delete\n\n\t enter your choice\t");
scanf("%d",&ch);
for(i=0,x=0;i<strlen(a);i++)
{
j=0;
temp=0;
if(a[i]==b[0])
{
pos=i;
while(a[i]==b[j])
{
temp=1;
i++;
j++;
}
}
if((j>=strlen(b))&&(temp==1))
{
p[x]=pos+1;
x++;
}
}
if(ch==1)
{
if(p[0]==0)
printf("\nsubstring not found");
else
{
printf("\nThe substring is at the position:\t");
for(i=0;i<x;i++)
printf("%d\t",p[i]);
}
printf("\n");
exit(0);
}
else if(ch==2)
{
if(p[0]!=0)
{
printf("\n string after the deletion\t");
pos=0;
for(x=0;p[x]!=0;x++)
{
for(i=pos;i<p[x]-1;i++)
printf("%c",a[i]);
pos=p[x]-1+strlen(b);
}
for(i=pos;a[i]!='\0';i++)
printf("%c",a[i]);
}
else
printf("deletion is not possible");
}
else
printf("invalid choice");
printf("\n");
}
![]() |
| output_substring search |
![]() |
| output_substring deletion |


No comments:
Post a Comment