#include <iostream>
#include <stdio.h>
#include <string.h>
int main()
{
char text[1000] = "i am groot we are groot";
char *word[1000] = { NULL, }; // 단어
int count[1000] = { 0, }; // 출현 횟수
int totalWordCount = 0; // 단어 수
char *w; // 읽은 단어
int i;
bool isRegistered;
printf("당신이 원하는 문장을 쓰세요. " );
// 테스트를 위해서 text를 위에서 미리 입력하고 밑에는 주석처리함
// scanf("%s",text,sizeof(text));
w = strtok(text, " "); // 단어 읽기
while (w) {
// 등록 여부
isRegistered = false;
for (i = 0; i < totalWordCount; i++)
{
if (strcmp(word[i], w) == 0)
{
isRegistered = true;
count[i]++;
break;
}
}
if(isRegistered)
{
break;
}
else
{
word[totalWordCount] = w; // 등록
count[totalWordCount] = 1;
totalWordCount++;
}
w = strtok(NULL, " "); // 다음 단어
}
for (i = 0; i < totalWordCount; i++)
{
printf("\n%s: %d", word[i], count[i]);
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKaW50IG1haW4oKQp7CiAgICBjaGFyIHRleHRbMTAwMF0gPSAiaSBhbSBncm9vdCB3ZSBhcmUgZ3Jvb3QiOwoJY2hhciAqd29yZFsxMDAwXSA9IHsgTlVMTCwgfTsgLy8g64uo7Ja0CiAgICBpbnQgY291bnRbMTAwMF0gPSB7IDAsIH07IC8vIOy2nO2YhCDtmp/siJgKCiAgICBpbnQgdG90YWxXb3JkQ291bnQgPSAwOyAvLyDri6jslrQg7IiYCiAgICBjaGFyICp3OyAvLyDsnb3snYAg64uo7Ja0CiAgICBpbnQgaTsKICAgIGJvb2wgaXNSZWdpc3RlcmVkOwoKICAgIHByaW50Zigi64u57Iug7J20IOybkO2VmOuKlCDrrLjsnqXsnYQg7JOw7IS47JqULiAiICk7CiAgICAvLyDthYzsiqTtirjrpbwg7JyE7ZW07IScIHRleHTrpbwg7JyE7JeQ7IScIOuvuOumrCDsnoXroKXtlZjqs6Ag67CR7JeQ64qUIOyjvOyEneyymOumrO2VqAogICAgLy8gc2NhbmYoIiVzIix0ZXh0LHNpemVvZih0ZXh0KSk7CiAgICAKICAgIHcgPSBzdHJ0b2sodGV4dCwgIiAiKTsgLy8g64uo7Ja0IOydveq4sAogICAgd2hpbGUgKHcpIHsKCiAgICAgICAgLy8g65Ox66GdIOyXrOu2gAogICAgICAgIGlzUmVnaXN0ZXJlZCA9IGZhbHNlOwogICAgICAgIGZvciAoaSA9IDA7IGkgPCB0b3RhbFdvcmRDb3VudDsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKHN0cmNtcCh3b3JkW2ldLCB3KSA9PSAwKQogICAgICAgICAgICB7CiAgICAgICAgICAgIAlpc1JlZ2lzdGVyZWQgPSB0cnVlOwogICAgICAgICAgICAJY291bnRbaV0rKzsKICAgICAgICAgICAgCWJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAogICAgICAgIGlmKGlzUmVnaXN0ZXJlZCkKICAgICAgICB7CiAgICAgICAgCWJyZWFrOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgIAl3b3JkW3RvdGFsV29yZENvdW50XSA9IHc7IC8vIOuTseuhnQogICAgICAgICAgICBjb3VudFt0b3RhbFdvcmRDb3VudF0gPSAxOwogICAgICAgICAgICB0b3RhbFdvcmRDb3VudCsrOwogICAgICAgIH0KCiAgICAgICAgdyA9IHN0cnRvayhOVUxMLCAiICIpOyAvLyDri6TsnYwg64uo7Ja0CiAgICB9CgogICAgZm9yIChpID0gMDsgaSA8IHRvdGFsV29yZENvdW50OyBpKyspCiAgICB7CiAgICAgICAgcHJpbnRmKCJcbiVzOiAlZCIsIHdvcmRbaV0sIGNvdW50W2ldKTsKICAgIH0KCiAgICByZXR1cm4gMDsKfQ==