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/ 8
BCSL305
DATA STRUCTURES LABORATORY
Lab Program-2 Vandana U Assistant Professor Dept of AI-DS 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 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 s1s2. 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. ALGORITHM: Step 1: Start Step 2: Read main string STR, pattern string PAT and replace string REP. Step 3: Search / find the pattern string PAT in the main string STR. Step 4: if PAT is found then replace all occurrences of PAT in main string STR with REP string. Step 5: if PAT is not found give a suitable error message. Step 6: Stop. #include<stdio.h> Reads the input string from the user void main(){ and stores it in STR. The format char STR[100],PAT[100],REP[100],ans[100]; specifier %[^\n] allows for reading a int i,j,c,m,k,flag=0; line of text including spaces until new printf("\nEnter the MAIN string: \n"); line character is entered. scanf(" %[^\n]", STR); printf("\nEnter a PATTERN string: \n"); scanf(" %s", PAT); printf("\nEnter a REPLACE string: \n"); scanf(" %s", REP); i = m = c = j = 0; Check for the pattern till the end of the string. End while ( STR[c] != '\0') { of the string is specified using NULL( \0 ) character. // Checking for Match if ( STR[m] == PAT[i]) { While traversing through the elements, if the i++; element in the string matches with the element m++; in the pattern, then , increment the index of flag=1; pattern and string and do the same check. If the if ( PAT[i] == '\0'){ entire pattern is matched, then set the flag to 1. //copy replace string in ans string for(k=0; REP[k] != '\0';k++,j++) If the character of the string and ans[j] = REP[k]; pattern has matched, then replace the i=0; pattern with the replacement string. c=m; } To check the next occurrence of the pattern, assign } the position where the next search has to start. else //mismatch { While checking if the pattern is present in the ans[j] = STR[c]; text, character by character., if the element did j++; not match , then directly place the original c++; character of the text into tht answer string. m = c; i=0; }} If there was no match then, flag would still be 0 if(flag==0){ printf("Pattern doesn't found!!!"); }else{ After getting all the characters in the ans[j] = '\0'; resultant string, we add \0 at the end printf("\nThe RESULTANT string is:%s\n" to show that the string has ended ,ans); }} i = m = c = j = 0; Check till we reach the end of the string while ( STR[c] != '\0') { // Checking for Match if ( STR[m] == PAT[i]) { Check whether the pattern(PAT) matches with Text(str) i++; m++; Increase the counter for pattern(PAT) and string(STR) flag=1; if ( PAT[i] == '\0'){ Counter for the replacement string . Check till the end of //copy replace string in ans string the pattern. for(k=0; REP[k] != '\0';k++,j++) ans[j] = REP[k]; Place the contents of replacement string(REP) in answer i=0; string(ans) c=m; } Again to check the pattern from the beginning and the } next character of the main string else //mismatch { ans[j] = STR[c]; If the str and pat did not match then, place the contents of the j++; Text(str) in the answer string(ans). c++; Increase counter of j and c. and initialize i=0(so that check m = c; i=0; starts from the beginning of the pattern and increase count of }} str , so that the next character can be checked if(flag==0){ printf("Pattern doesn't found!!!"); If flag is still zero, pattern did not match with string at all 1ST RUN: Text : Angry Man Pattern: Man Replacement: Bird 2 ndRun: Text : Data Learning Pattern: Deep Replacement: Machine