0% found this document useful (0 votes)
16 views8 pages

Program 2

2nd program
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)
16 views8 pages

Program 2

2nd program
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/ 8

Program 2.

Develop a Program in C for the following operations on Strings


Program

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.
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.
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 casePAT does not exist in STR.Support the
program with functions for each of the above operations. Don't use Built-in
functions.

#include<stdio.h>
#include<string.h>
char str[100], pat[50],
rep[50], ans[100];
int i, j, c, m, k, flag=0;
void stringmatch()
{
i = m = c = j = 0;
while(str[c] != '\0')
{
if(str[m] ==
pat[i]) // ...... matching
{
i++; m+
+;
if(pat[i] ==
'\0') //.....found
occurrences.
{
flag =
1;
for(k =
0; rep[k] != '\0'; k++, j+
+)
ans[j] = rep[k];
i = 0;
c = m;
}
}
else
{
ans[j++] =
str[c++];
m = c;
i = 0;
}
}
ans[j] = '\0';
}
void main()
Dept of CSE, PESITM
Shivamogga
#include<stdio.h>
#include<string.h>
char str[100], pat[50], rep[50], ans[100];
int i, j, c, m, k, flag=0;
void stringmatch( )
{
i = m = c = j = 0;
while(str[c] != '\0')
{
if(str[m] == pat[i]) // ...... matching
{
i++; m++;
if(pat[i] == '\0') //.....found occurrences.
{
flag = 1;

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


ans[j] = rep[k];
i = 0;
c = m;
}
}
else
{
ans[j++] = str[c++];
m = c;
i = 0;
}
}
ans[j] = '\0';
}
void main()

{
printf("\nEnter a main string \n");
gets(str);
printf("\nEnter a pattern string \n");

gets(pat);
printf("\nEnter a replace string \n");

gets(rep);
stringmatch();
if(flag == 1)
printf("\nThe resultant string is\n %s" , ans);
else
printf("\nPattern string NOT found\n");
}

Output:
Run1
Enter a main string
apple is an apple
Enter a pattern string
app
Enter a replace string
coup
The resultant string is
couple is an couple
Run1
Enter a main string
apple is an apple
Enter a pattern string
dfg
Enter a replace string
coup
Pattern string NOT found

You might also like