Here is the C program for the above.
#include <stdio.h>
#include <string.h>
int main()
{
char str[500];
char word1[50];
char word2[50];
char *ptr = NULL;
int i = 0, found1 = 0;
printf("Enter the desired string :\n");
fgets(str, sizeof(str), stdin);
str[strlen(str) - 1] = '\0'; //over ride the new line.
printf("\nEnter the two word one by one.\n");
scanf("%s", word1);
scanf("%s", word2);
ptr = strtok(str, " ");
while (ptr) {
if (found1 == 0) {
if (!strcmp(ptr, word1)) {
found1 = 1;
i = 0;
}
} else if (found1) {
if (!strcmp(ptr, word2)) {
break;
}
i++;
}
ptr = strtok(NULL, " ");
}
printf("Min Distance: %d\n", i);
return 0;
}