//program for string matching using greedy algorithm 

#include<stdio.h>
#include<stdbool.h>

// Function to find match between given string and pattern 
bool stringMatching(char string[], char pattern[]) 
{ 
	//calculate length of given string 
	int n = strlen(string);  

	//calculate length of given pattern  
	int m = strlen(pattern);  

	// indices of string and pattern 
	int i = 0, j = 0;  

	// Greedy loop to match 
	while(i < n && j < m) { 
		if(string[i] == pattern[j]) { 
			i++; 
			j++; 
		} 
		// Go back one position in string 
		else { 
			i = i - j + 1; 
			j = 0; 
		}	 
	} 

	// Check whether pattern is found or not 
	if(j == m) 
		return true; 
	
	return false; 
} 

// Driver code 
int main() 
{ 
	char string[] = "ababcab"; 
	char pattern[] = "cab"; 
	
	bool flag = stringMatching(string, pattern); 
	
	if (flag) 
		printf("Pattern matches"); 
	else
		printf("Pattern do not matches"); 
		
	return 0; 
}
Corda A:ababcab Corda B:cab
Costruisci un programma per 'String Matching' usando Greedy Algorithm.