Program 2: Developa 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.
Department of CSE- Data Science
3.
Procedure
Step 1: Input
•Read STR (main string)
• Read PAT (pattern to find)
• Read REP (replacement string)
Step 2: Initialization
• Set i = 0 (index for STR)
• Set j = 0 (index for ANS, the result string)
• Set flag = 0 (to track if any match is found)
Department of CSE- Data Science
4.
Step 3: PatternMatching Loop
• While STR[i] is not end of string:
• Check if PAT matches at position i in STR
• If match found:
• Copy REP to ANS
• Advance i by length of PAT
• Set flag = 1
• If no match:
•Copy current character from STR to ANS
•Increment i and j
Department of CSE- Data Science
5.
Step 4: Output
•If flag == 0, print "Pattern not found"
• Else, print ANS as the modified string
Department of CSE- Data Science
6.
Program
#include <stdio.h>
void read(charSTR[], char PAT[], char REP[]);
void match(const char STR[], const char PAT[], const char REP[], char ANS[]);
int main()
{
char STR[100], PAT[100], REP[100], ANS[300];
read(STR, PAT, REP);
match(STR, PAT, REP, ANS);
return 0;
}
Department of CSE- Data Science
7.
void read(char STR[],char PAT[], char REP[])
{
printf("Enter the main string STR: ");
gets(STR);
printf("Enter the pattern string PAT: ");
gets(PAT);
printf("Enter the replace string REP: ");
gets(REP);
}
Department of CSE- Data Science
8.
void match(const charSTR[], const char PAT[], const char REP[], char ANS[])
{
int i = 0, j = 0, k, m, flag = 0;
while (STR[i] != '0')
{
m = i;
k = 0;
// Check for pattern match
while (STR[m] == PAT[k] && PAT[k] != '0’)
{
m++;
k++;
}
Department of CSE- Data Science
9.
// If patternfound
if (PAT[k] == '0')
{
flag = 1;
k = 0;
while (REP[k] != '0')
{
ANS[j++] = REP[k++];
}
i = m;
}
else
{
ANS[j++] = STR[i++];
}
}
Department of CSE- Data Science
10.
ANS[j] = '0';// Null-terminate result string
if (flag == 0)
printf("Pattern not found.n");
else
printf("Resultant string is: %sn", ANS);
}
Department of CSE- Data Science
Editor's Notes
#1 Welcome students to the very first Data Structures lab.
Explain that this lab will form the backbone for their journey into problem-solving, efficiency, and programming.
Highlight how labs are about hands-on practice rather than just theory.
#2 Emphasize that students already know C basics, now they will apply it to handle structured data.
Show how DS labs will help them solve real-world problems efficiently.
Stress on logical thinking: DS is about how data is stored and accessed.
#3 Relate DS to their specialization: Data Science.
Example: Handling millions of records in a dataset requires efficient structures.
Sorting, searching, and graph algorithms are building blocks of machine learning pipelines.
#4 Engage students with reflection questions.
Push them to think: what if we need dynamic memory?
This prepares ground for Linked Lists in upcoming labs.
#5 Engage students with reflection questions.
Push them to think: what if we need dynamic memory?
This prepares ground for Linked Lists in upcoming labs.
#6 Engage students with reflection questions.
Push them to think: what if we need dynamic memory?
This prepares ground for Linked Lists in upcoming labs.
#7 Engage students with reflection questions.
Push them to think: what if we need dynamic memory?
This prepares ground for Linked Lists in upcoming labs.
#8 Engage students with reflection questions.
Push them to think: what if we need dynamic memory?
This prepares ground for Linked Lists in upcoming labs.
#9 Engage students with reflection questions.
Push them to think: what if we need dynamic memory?
This prepares ground for Linked Lists in upcoming labs.
#10 Engage students with reflection questions.
Push them to think: what if we need dynamic memory?
This prepares ground for Linked Lists in upcoming labs.