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

#define MAX_WORDS_COUNT 1000

int FindRegisteredWordIndex(char* word, char** registeredWords, int totalWordsCount)
{
	int index = -1;
	int i;
	for (i = 0; i < totalWordsCount; i++)
    {
        if (strcmp(registeredWords[i], word) == 0)
        {
        	index = i;
        }
    }
    return index;
}
 
void PrintRegisteredWords(char** registeredWords, int* wordCount, int totalWordsCount)
{
	int i;
    for (i = 0; i < totalWordsCount; i++)
    {
        printf("\n%s: %d", registeredWords[i], wordCount[i]);
    }
}
 
int main()
{
    char text[MAX_WORDS_COUNT] = "i am groot we are groot";
	char *registeredWords[MAX_WORDS_COUNT] = { NULL, }; // 단어
    int wordCount[MAX_WORDS_COUNT] = { 0, }; // 출현 횟수
 
    int totalWordsCount = 0; // 단어 수
    char *currentWord; // 읽은 단어
    int wordIndex;
 
    printf("당신이 원하는 문장을 쓰세요. " );
    // 테스트를 위해서 text를 위에서 미리 입력하고 밑에는 주석처리함
    // scanf("%s",text,sizeof(text));
 
    currentWord = strtok(text, " "); // 단어 읽기
    while (currentWord) {
        wordIndex = FindRegisteredWordIndex(currentWord, registeredWords, totalWordsCount);

        if(wordIndex != -1)
        {
        	wordCount[wordIndex]++;
        }
        else
        {
        	registeredWords[totalWordsCount] = currentWord; // 등록
            wordCount[totalWordsCount] = 1;
            totalWordsCount++;
        }
 
        if(totalWordsCount == MAX_WORDS_COUNT)
        {
        	printf("최대 저장 갯수에 도달했습니다.\n");
			break;
        }
 
        currentWord = strtok(NULL, " "); // 다음 단어
    }

	PrintRegisteredWords(registeredWords, wordCount, totalWordsCount);
    return 0;
}