There are two strings s1 and s2. Find the maximum of percentage word matching from s1 to s2 and s2 to s1.
Where, percentage word matching= (Total words matched in s2/total number of words in s1) *100
Working code :
Simple Logic : tokenize One String into words.
Check whether word is part of another string.
PS : coding is tricky here as it consists of Strings and String functions.
First try by your self and then check here for reference.
Thanks to Dhaval for suggesting this approach and Article
#include <stdio.h>
#include <string.h>
int wordCount(char s1[]){
int i,wc=1;
for(i=0;i<=strlen(s1);i++){
if (s1[i]==’ ‘ ) wc++;
}
return wc;
}//wordCount
int findPercentage(char s1[],char s2[],int n){
int i=0,j=0,mc=0,wc1,wc2;
char w1[n],w2[n];
for(i=0;i<=strlen(s1);i++,j++){
w1[j]=s1[i];
if(s1[i]==’ ‘||s1[i]==”){w1[j]=”;j=-1;
if(strstr(s2,w1))mc++;
}//if
}//for
wc1=wordCount(s1);
wc2=wordCount(s2);
printf(“Common words = %d nC1 = %d % nC2 = %d %” ,mc, (mc*100)/wc1,(mc*100)/wc2);
}//findPercentage
int main(void) {
int max;
char s1[]=”This is sparta”;
char s2[]=”sparta is a country”;
max=strlen(s1)>strlen(s2)? strlen(s1): strlen(s2);
findPercentage(s1,s2,max);
return 0;
}
Working Code you can find at : http://ideone.com/e.js/EdL7NH