1 / 6

program 2

Design, Develop and Implement a Program in C for the following operations on Strings <br>a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP) <br>b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR. <br>Report suitable messages in case PAT does not exist in STR <br>Support the program with functions for each of the above operations. Don't use Built-in functions.<br>

Geetha6
Download Presentation

program 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design, Develop and Implement 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.

  2. #include<stdio.h> • char str[100], pat[50], rep[50], ans[100]; • inti, j, c, m, k, flag=0; • void stringmatch() • { • i = m = c = j = 0; • while(str[c] != '\0') • {

  3. if(str[m] == pat[i]) // ...... matching • { • i++; m++; • if(pat[i] == '\0') //.....found occurrences. • { • flag = 1; • //.... copy replace string in ans string. • for(k = 0; rep[k] != '\0'; k++, j++) • ans[j] = rep[k]; • i = 0; • c = m; • } • } // if ends

  4. m=9 m=6 m=7 m=2 m=3 m=8 m=0 m=5 m=1 m=4 N STR R A ‘\0’ A G Y M N i=0 i=1 i=2 i=3 PAT N A ‘\0’ M REP B I R D ‘\0’ R ‘\0’ R I ‘D A G Y B N ANS

  5. else //... mismatch • { • ans[j] = str[c]; • j++; • c++; • m = c; • i = 0; • }//else ends • } //end of while • ans[j] = '\0'; • } //end stringmatch()

  6. void main() • { • printf("\nEnter a main string \n"); • gets(str); • printf("\nEnter a pattern string \n");gets(pat); • printf("\nEnter a replace string \n"); • gets(rep); • stringmatch(); • if(flag == 1) • printf("\nThe resultant string is\n %s" , ans); • else • printf("\nPattern doesn’t found\n"); • } // end of main

More Related