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. Repost 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.
Operations on String without using built-in functions in C
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.
//Operations on String without using built-in functions in C #include<stdio.h> #include<conio.h> //Declarations char str[100], pat[50], rep[50], ans[100]; int i, j, c, m, k, flag=0; void stringmatch() { i = m = c = j = 0; while(str[c] ! = '\0') { 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. else { //... mismatch ans[j] = str[c]; j++; c++; m = c; i = 0; }//else ends } //end of while ans[j] = '\0'; } //end stringmatch() void main() { clrscr(); printf("\nEnter a main string \n"); gets(str); printf("\nEnter a pattern string \n"); flushall(); gets(pat); printf("\nEnter a replace string \n"); flushall(); gets(rep); stringmatch(); if(flag = = 1) printf("\nThe resultant string is\n %s" , ans); else printf("\nPattern string NOT found\n"); getch(); } // end of main //Operations on String without using built-in functions in C
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