0% found this document useful (0 votes)
24 views3 pages

Program 2

Uploaded by

sowmyabkdaa
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)
24 views3 pages

Program 2

Uploaded by

sowmyabkdaa
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/ 3

Data Structures Lab Manual 18CSL38

2. Design, Develop and Implement a Program in C for the following operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. 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
Support the program with functions for each of the above operations. Don't use Built-in
functions.

PROGRAM
#include<stdio.h>
#include<conio.h>
void read_data();
void search_replace();
char str[100],pat[100],rep[100],update[100];

void main()
{
clrscr();
read_data();
search_replace();
getch();
}
void read_data()
{
printf("\nEnter a string \n");
gets(str);
printf("\nEnter a search string \n");
gets(pat);
printf("\nEnter a replace string \n");
gets(rep);
}
void search_replace()
{
int i=0,j=0,c=0,m=0,k, flag=0;
while(str[c] != '\0')
{
if(str[m]==pat[i])
{
i++;
m++;
if(pat[i] == '\0') //.....found occ
{
flag=1;
for(k=0; rep[k]!= '\0';k++,j++)
update[j] = rep[k];
i=0;

AMCEC, Bangalore Page 1


Data Structures Lab Manual 18CSL38

c=m;
}
}
else //... mismatch
{
update[j] = str[c];
j++;
c++;
m = c;
i=0;
}
}//while
if(flag==1)
{
update[j] = '\0';
printf("\nThe resultant string is\n%s" ,update);
}
else
printf("String not-found\n");
}

OUTPUT:

Test Search Replace


Main String Updated String
Case Pattern String
Hi Welcome to 10 Hi Welcome to CBSE Scheme
1 10 CBSE
Scheme DS Lab DS Lab

AMCEC, Bangalore Page 2

You might also like