0% found this document useful (0 votes)
12 views2 pages

Program 2

The document describes a program in C that reads a main string, pattern string, and replace string from the user. It then performs a pattern matching operation to find and replace all occurrences of the pattern string in the main string with the replace string, and outputs a suitable message if the pattern is not found.

Uploaded by

anithag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Program 2

The document describes a program in C that reads a main string, pattern string, and replace string from the user. It then performs a pattern matching operation to find and replace all occurrences of the pattern string in the main string with the replace string, and outputs a suitable message if the pattern is not found.

Uploaded by

anithag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

 Design, Develop and Implement a Program in C for the following

operations on Strings
1. Read a main String (STR), a Pattern String (PAT) and a Replace
String (REP)
2. Perform Pattern Matching Operation: Find and Replace all
occurrences of PAT in STR with REP if PAT exists in STR. Report
suitable messages in case PAT does not exist in STR

#include<stdio.h>

void main()

char s[20],pat[20],rep[20],ans[30];

int i,j,k,l,flag;

printf("\nEnter string:");

scanf("%s",s);

printf("\nEnter pattern:");

scanf("%s",pat);

printf("\nEnter replacement:");

scanf("%s",rep);

for(i=0,k=0;s[i]!='\0';i++)

flag=1;

for(j=0;pat[j]!='\0';j++)

if(s[i+j]!=pat[j])

flag=0;
l=j;

if(flag)

for(j=0;rep[j]!='\0';j++,k++)

ans[k]=rep[j];

i+=l-1;

else

ans[k++]=s[i];

ans[k]='\0';

printf("%s",ans);

You might also like