0% found this document useful (0 votes)
10 views

2_StringMatch

String match ds program vtu 3rd sem

Uploaded by

abhinav2349r
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)
10 views

2_StringMatch

String match ds program vtu 3rd sem

Uploaded by

abhinav2349r
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

/* Lab Program 2

2. Develop 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.

*/
// Header files
#include <stdio.h>

#define MAX 100

//Prototype
int StringMatch(char [], char [], char [], char []);

//Main function
int main()
{
char s[MAX]={0}, p[MAX]={0}, r[MAX]={0}, final[MAX]={0};
int found;

printf("Enter Source String : \n");


gets(s);
printf("Enter Pattern String : \n");
gets(p);
printf("Enter Replace String : \n");
gets(r);
found = StringMatch(s, p, r, final);
if (found == 1)
{
printf("The Final String is : \n");
puts(final);
}
else printf("Search string Not Found\n");
return 0;
}

// StringMatch function definition


int StringMatch(char s[], char p[], char r[], char f[])
{
int i, j, k, m, t; // i-index src, j-index pattern, k-index replace, t-index final
int found = 0;
j = m = i = t = 0;
while (s[i] != '\0')
{
if (s[m++] == p[j++]) // check for matching
{
if (p[j] == '\0') // pattern found
{
// copy replace string in final string
for(k=0; r[k]!='\0'; k++,t++)
f[t] = r[k];
j = 0;
i = m;
found = 1;
}
}
else // mismatch
{
f[t++] = s[i++];
m = i;
j = 0;
}
}
return found;
}

/*
OUTPUT:

[root@localhost 2022batchDSLab]# ./a.out


Enter Source String :
Welcome to bit, cse
Enter Pattern String :
bit
Enter Replace String :
mit
The Final String is :
Welcome to mit, cse

[root@localhost 2022batchDSLab]# ./a.out


Enter Source String :
Bangalore is beautiful but bangalore is crowded
Enter Pattern String :
Bangalore
Enter Replace String :
Mangalore
The Final String is :
Mangalore is beautiful but bangalore is crowded

[root@localhost 2022batchDSLab]# ./a.out


Enter Source String :
Bangalore is called silicon city, Bangalore is also called as garden city
Enter Pattern String :
Bangalore
Enter Replace String :
Mangalore
The Final String is :
Mangalore is called silicon city, Mangalore is also called as garden city

[root@localhost 2022batchDSLab]# ./a.out


Enter Source String :
temple run is boring
Enter Pattern String :
bor
Enter Replace String :
interest
The Final String is :
temple run is interesting

root@localhost 2022batchDSLab]# ./a.out


Enter Source String :
life is easy
Enter Pattern String :
boring
Enter Replace String :
nice
Search string Not Found

*/

You might also like