BCSL305 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.
#include<stdio.h>
char str[50], pat[20], rep[20], res[50];
int c = 0, m = 0, i = 0, j = 0, k, flag = 0;
void stringmatch()
{
while (str[c] != '\0')
{
if (str[m] == pat[i])
{
i++;
m++;
if (pat[i] == '\0')
{
flag = 1;
for (k = 0; rep[k] != '\0'; k++, j++)
{
res[j] = rep[k];
}
i = 0;
c = m;
}
}
else
{
res[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
res[j] = '\0';
}
void main()
{
printf("Enter the main string:");
gets(str);
printf("\nEnter the pat string:");
gets(pat);
printf("\nEnter the replace string:");
gets(rep);
printf("\nThe string before pattern match is:\n %s", str);
stringmatch();
if (flag == 1)
printf("\nThe string after pattern match and replace is: \n %s ", res)
else
printf("\nPattern string is not found");
}
OUTPUT:
**************************OUTPUT 1*******************************
Enter the main string:Designed by vtucode
Enter the pat string:vtucode
Enter the replace string:Braham
The string before pattern match is:
Designed by vtucode
The string after pattern match and replace is:
Designed by Braham
**************************OUTPUT 2*******************************
Enter the main string:Designed by Developer
Enter the pat string:vtucode
Enter the replace string:Braham
The string before pattern match is:
Designed by Developer
Pattern string is not found
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Post Comment
search... Search
Note: If you have any useful engineering related study materials with you, kindly share with
us, it will be more useful to other students those who are financially troubled but deserving to
learn...
UPLOAD NOTES
Android App Available Download Now !
Join us for Latest Updates!
DOWNLOAD APP
Recent Posts
Deep Learning 21CS743
NoSQL Database 21CS745
Advanced AI and ML 21AI71
Software Architecture and Design 21CS741
Internet of Things 21CS735
Cryptography and Network Security 21CS733
Digital Image Processing 21CS732
Cloud Computing 21CS72
Big Data Analytics 21CS71
Information Retrieval BAI515B
Environmental Studies BESK508
Research Methodology & IPR BRMK557
Computer Vision BAI515A
Distributed Systems BCS515D
Unix System Programming BCS515C
Artificial Intelligence BCS515B
Computer Graphics BAI515A
Introduction to C Programming BESCK104E-204E
Introduction to Electronics Communication BESCK104C-204C
Computer Networks BCS502
Categories
2022 Scheme
3rd semester 2021 scheme
3rd semester 2022 scheme
4th semester 2021 scheme
4th semester 2022 scheme
5th semester 2021 scheme
5th semester 2022 scheme
6th semester 2021 scheme
7th semester 2021 scheme
AI&ML-DS
CSE-ISE
First Year
Archives
2024
2023
VTU Links Quick Links About Us
VTU Result First Year About Us
VTU Circular CSE-ISE Contact Us
VTU Model Paper AI&ML-DS Disclaimer
VTU Examination Upload Notes Privacy Policy
Academic Calendar SGPA-CGPA Calculator Terms and Conditions
Designed & Developed by Braham Kumar | Copyright © 2023 - 2024 [Link] | All Right
Reserved.