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

About The Experiment: Strings Are Actually One-Dimensional Array of Characters Terminated by A Null Character '/0'

The document describes a C program to perform pattern matching and string replacement operations on strings. The program takes in a main string, a pattern string, and a replacement string as input. It finds all occurrences of the pattern string in the main string and replaces them with the replacement string, outputting the resulting string. If the pattern is not found, a message is displayed. The program uses functions to implement the string operations without built-in string functions. Sample outputs showing replacements and pattern not found messages are also provided.

Uploaded by

Karthik Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

About The Experiment: Strings Are Actually One-Dimensional Array of Characters Terminated by A Null Character '/0'

The document describes a C program to perform pattern matching and string replacement operations on strings. The program takes in a main string, a pattern string, and a replacement string as input. It finds all occurrences of the pattern string in the main string and replaces them with the replacement string, outputting the resulting string. If the pattern is not found, a message is displayed. The program uses functions to implement the string operations without built-in string functions. Sample outputs showing replacements and pattern not found messages are also provided.

Uploaded by

Karthik Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program -02

Design, Develop and Implement a Program in C for the following operations on Strings
Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
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.

ABOUT THE EXPERIMENT:


Strings are actually one-dimensional array of characters terminated by a null character '\0'.
Thus a null-terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing the string
is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement as follows:
char greeting[] = "Hello";
C language supports a wide range of built-in functions that manipulate null-terminated strings as
follows:
strcpy(s1, s2); Copies string s2 into string s1.
strcat(s1, s2); Concatenates string s2 onto the end of string s1. strlen(s1); Returns the length of
string s1.
strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 I s1>s2.
strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.

PROGRAM CODE:

#include<stdio.h>

char STR[100], PAT[50], REP[50], ANS[100];


int i, j, k, m, c, flag;
void stringmatch()
{
i = j = m = c = 0;
while(STR[c] != '\0')
{
if(STR[m] == PAT[i])
{
i++;
m++;
if(PAT[i] =='\0')
{
flag=1;
for(k=0; REP[k] != '\0'; j++, k++)
{
ANS[j] = REP[k];
}
i = 0;
c = m;
}
}
else
{
ANS[j] = STR[c];
}
i = 0;
c = m;
}
}
else
{
ANS[j] = STR[c];
j++;
c++;
m = c;
i = 0;
}
}
ANS[j] = '\0';
}
void main()
{
printf("Enter the Main String\n");
gets(STR);
printf("Enter the Pattern String\n");
gets(PAT);
printf("Enter the Replace String\n");
gets(REP);
stringmatch();
if(flag ==1)
{
printf("The Resultant String is %s",ANS);
}
else
{
printf("Pattern Not Found");
}
return 0;
}

SAMPLE OUTPUT:
RUN 1:
Enter a main string Test
Enter a pattern string Te
Enter a replace string Re
The resultant string is Rest
RUN 2:
Enter a main string
This is Data Structure lab Enter a pattern string Data Structure
Enter a replace string Data structure with C
The resultant string is
This is Data structure with C lab
RUN 3:
Enter a main string
This is Data Structure lab Enter a pattern string Date
Enter a replace string DATA
Pattern string NOT found

You might also like