String Indexing



We use the program of pattern matching to understand string indexing
    
#include<stdio.h>
int main(){
char string[50],pattern[25];
printf("Enter the string: \n");
fgets(string,50,stdin);
// the code could break if using multiple words , then in order to temrinate the string on "\n" use :
// string[strcspn(string,"\n")]='\0';

printf("Enter the patterns: \n");
fgets(pattern,25,stdin);
// pattern[strcspn(pattern,"\n")]='\0';

int i=0,k=0,j;
while(string[i]!='\0'){
if(string[i]==pattern[0]){
j=1;

// check if either pattern or string has not reached its termination
while(pattern[j]!='\0' && string[i+j]!='\0' && string[i+j]==pattern[j]){
k=1; // used as a flag
j++;
}
if(pattern[j]=='\0'){
printf("\nPattern found at index: %d",i+1);
}
}
i++;
}
if(k==0){
if(string[i+j]=='\0'){
printf("\n Pattern not found ! ");
}
}
}
    
  



Assignment 1

A small assignment to create a c program to perform all these functions
Making it an actual ADT library , [ try using switch ]



Assignment 2

Similarly create functions for insert, delete, and replace
hint: replace could just be done by deletion followed by insertion
For this program , we can just make functions do everything
Further on true ADT just takes input while invocation itself



Pattern Matching