#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct WordCollection
{
	size_t NumWords;
	char **Words;
};

void splitWord(char *str, struct WordCollection *wc)
{
	char *c;
	char **currentWord;

	c = str;

	wc->NumWords = 1;

	while (*c != '.')
	{
		if (*c == ' ')
		{
			wc->NumWords++;
		}

		c++;
	}

	*c = '\0';

	wc->Words = (char**)malloc(wc->NumWords * sizeof(char*));

	c = strtok(str, " ");

	currentWord = wc->Words;

	while (c)
	{
		*currentWord = c;
		currentWord++;

		c = strtok(NULL, " ");
	}
}

int myComp(const void *p1, const void *p2)
{
	return strcmp(*(const char**)p1, *(const char**)p2);
}

int main(void)
{
	char a[] = { 'w', 'o', 'r', 'n', 'g', ' ', 'w', 'o', 'r', 'd', '.' };
	char b[] = { 'c', 'o', 'r', 'r', 'e', 'c', 't', ' ', 'w', 'o', 'r', 'd', '.' };

	struct WordCollection a1, b1;
	struct WordCollection *pSmaller, *pBigger;

	size_t i;

	splitWord(a, &a1);
	splitWord(b, &b1);

	if (a1.NumWords <= b1.NumWords)
	{
		pSmaller = &a1;
		pBigger = &b1;
	}
	else
	{
		pSmaller = &b1;
		pBigger = &a1;
	}

	qsort(pBigger->Words, pBigger->NumWords, sizeof(char*), myComp);

	for (i = 0; i < pSmaller->NumWords; i++)
	{
		void *res = bsearch(&pSmaller->Words[i], pBigger->Words, pBigger->NumWords, sizeof(char*), myComp);
		if (res)
		{
			printf("Found: %s", pSmaller->Words[i]);
		}
	}

	free(a1.Words);
	free(b1.Words);

	return 0;
}
