0% found this document useful (0 votes)
7 views2 pages

C Program for String Pattern Replacement

The document contains a C program that replaces occurrences of a specified pattern string within a main string with a replacement string. It reads the main string, pattern, and replacement from user input, checks for matches, and constructs a new resultant string. If the pattern is not found, it notifies the user; otherwise, it displays the modified string.

Uploaded by

shilpakc
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)
7 views2 pages

C Program for String Pattern Replacement

The document contains a C program that replaces occurrences of a specified pattern string within a main string with a replacement string. It reads the main string, pattern, and replacement from user input, checks for matches, and constructs a new resultant string. If the pattern is not found, it notifies the user; otherwise, it displays the modified string.

Uploaded by

shilpakc
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

#include<stdio.

h>

#include<string.h>

void main()

char STR[100],PAT[100],REP[100],ans[200];

int i,j,c,m,k,flag=0;

printf("\nEnter the MAIN string: \n");

fgets(STR, sizeof(STR), stdin); // ? reads full line including spaces

STR[strcspn(STR, "\n")] = '\0'; // ? removes newline if present

printf("\nEnter a PATTERN string: \n");

scanf("%s", PAT);

printf("\nEnter a REPLACE string: \n");

scanf("%s", REP);

i = m = c = j = 0;

while ( STR[c] != '\0')

// Checking for Match

if ( STR[m]==PAT[i])

i++; m++;

flag=1;

if ( PAT[i] == '\0')

{
// copy replace string in ans string

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

ans[j] = REP[k];

i=0;

c=m;

else // mismatch

ans[j] = STR[c];

j++; c++;

m = c; i=0;

if(flag==0)

printf("Pattern not found!!!\n");

else

ans[j] = '\0';

printf("\nThe RESULTANT string is: %s\n" ,ans);

You might also like