fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAX_WORDS_COUNT 1000
  6.  
  7. int FindRegisteredWordIndex(char* word, char** registeredWords, int totalWordsCount)
  8. {
  9. int index = -1;
  10. int i;
  11. for (i = 0; i < totalWordsCount; i++)
  12. {
  13. if (strcmp(registeredWords[i], word) == 0)
  14. {
  15. index = i;
  16. }
  17. }
  18. return index;
  19. }
  20.  
  21. void PrintRegisteredWords(char** registeredWords, int* wordCount, int totalWordsCount)
  22. {
  23. int i;
  24. for (i = 0; i < totalWordsCount; i++)
  25. {
  26. printf("\n%s: %d", registeredWords[i], wordCount[i]);
  27. }
  28. }
  29.  
  30. int main()
  31. {
  32. char text[MAX_WORDS_COUNT] = "i am groot we are groot";
  33. char *registeredWords[MAX_WORDS_COUNT] = { NULL, }; // 단어
  34. int wordCount[MAX_WORDS_COUNT] = { 0, }; // 출현 횟수
  35.  
  36. int totalWordsCount = 0; // 단어 수
  37. char *currentWord; // 읽은 단어
  38. int wordIndex;
  39.  
  40. printf("당신이 원하는 문장을 쓰세요. " );
  41. // 테스트를 위해서 text를 위에서 미리 입력하고 밑에는 주석처리함
  42. // scanf("%s",text,sizeof(text));
  43.  
  44. currentWord = strtok(text, " "); // 단어 읽기
  45. while (currentWord) {
  46. wordIndex = FindRegisteredWordIndex(currentWord, registeredWords, totalWordsCount);
  47.  
  48. if(wordIndex != -1)
  49. {
  50. wordCount[wordIndex]++;
  51. }
  52. else
  53. {
  54. registeredWords[totalWordsCount] = currentWord; // 등록
  55. wordCount[totalWordsCount] = 1;
  56. totalWordsCount++;
  57. }
  58.  
  59. if(totalWordsCount == MAX_WORDS_COUNT)
  60. {
  61. printf("최대 저장 갯수에 도달했습니다.\n");
  62. break;
  63. }
  64.  
  65. currentWord = strtok(NULL, " "); // 다음 단어
  66. }
  67.  
  68. PrintRegisteredWords(registeredWords, wordCount, totalWordsCount);
  69. return 0;
  70. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
당신이 원하는 문장을 쓰세요. 
i: 1
am: 1
groot: 2
we: 1
are: 1