#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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBNQVhfV09SRFNfQ09VTlQgMTAwMAoKaW50IEZpbmRSZWdpc3RlcmVkV29yZEluZGV4KGNoYXIqIHdvcmQsIGNoYXIqKiByZWdpc3RlcmVkV29yZHMsIGludCB0b3RhbFdvcmRzQ291bnQpCnsKCWludCBpbmRleCA9IC0xOwoJaW50IGk7Cglmb3IgK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uZigiJXMiLHRleHQsc2l6ZW9mKHRleHQpKTsKIAogICAgY3VycmVudFdvcmQgPSBzdHJ0b2sodGV4dCwgIiAiKTsgLy8g64uo7Ja0IOydveq4sAogICAgd2hpbGUgKGN1cnJlbnRXb3JkKSB7CiAgICAgICAgd29yZEluZGV4ID0gRmluZFJlZ2lzdGVyZWRXb3JkSW5kZXgoY3VycmVudFdvcmQsIHJlZ2lzdGVyZWRXb3JkcywgdG90YWxXb3Jkc0NvdW50KTsKCiAgICAgICAgaWYod29yZEluZGV4ICE9IC0xKQogICAgICAgIHsKICAgICAgICAJd29yZENvdW50W3dvcmRJbmRleF0rKzsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAJcmVnaXN0ZXJlZFdvcmRzW3RvdGFsV29yZHNDb3VudF0gPSBjdXJyZW50V29yZDsgLy8g65Ox66GdCiAgICAgICAgICAgIHdvcmRDb3VudFt0b3RhbFdvcmRzQ291bnRdID0gMTsKICAgICAgICAgICAgdG90YWxXb3Jkc0NvdW50Kys7CiAgICAgICAgfQogCiAgICAgICAgaWYodG90YWxXb3Jkc0NvdW50ID09IE1BWF9XT1JEU19DT1VOVCkKICAgICAgICB7CiAgICAgICAgCXByaW50Zigi7LWc64yAIOyggOyepSDqsK/siJjsl5Ag64+E64us7ZaI7Iq164uI64ukLlxuIik7CgkJCWJyZWFrOwogICAgICAgIH0KIAogICAgICAgIGN1cnJlbnRXb3JkID0gc3RydG9rKE5VTEwsICIgIik7IC8vIOuLpOydjCDri6jslrQKICAgIH0KCglQcmludFJlZ2lzdGVyZWRXb3JkcyhyZWdpc3RlcmVkV29yZHMsIHdvcmRDb3VudCwgdG90YWxXb3Jkc0NvdW50KTsKICAgIHJldHVybiAwOwp9