REPLACING THE GIVEN STRING
#include”stdio.h”
#include “conio.h”
#include
void main()
{
char a[50],b[50],c[50],d[50];
int i,j,e,l,k,f=0;
clrscr();
printf("Enter the String : ");
gets(a);
printf("Enter the Substring : ");
gets(b);
printf("Enter the string to be replace : ");
gets(c);
e=0;
i=0;
while(a[i]!='\0')
{
j=0;
k=i;
while(b[j]==a[k]&&b[j]!='\0')
{
j++;
k++;
}
if(a[k]=='\0'||a[k]==' ')
{
if(b[j]=='\0')
{
l=0;
i=k;
while(c[l]!='\0')
{
d[e]=c[l];
e++;
l++;
f=l;
}
}
else
{
d[e]=a[i];
e++;
i++;
}
}
else
{
d[e]=a[i];
e++;
i++;
}
}
d[e]='\0';
if(f==0)
printf("\nThe string is not found");
else
{
printf("Replaced string: ");
printf("%s",d);
}
getch();
}
Output will be
Enter the String : this is a hotel
Enter the Substring : hotel
Enter the string to be replace : college
Replaced string: this is a college
Enter the String : this is a wall
Enter the Substring : hai
Enter the string to be replace : hi
The string is not found
No comments:
Post a Comment