fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7. char text[1000] = "i am groot we are groot";
  8. char *word[1000] = { NULL, }; // 단어
  9. int count[1000] = { 0, }; // 출현 횟수
  10.  
  11. int totalWordCount = 0; // 단어 수
  12. char *w; // 읽은 단어
  13. int i;
  14. bool isRegistered;
  15.  
  16. printf("당신이 원하는 문장을 쓰세요. " );
  17. // 테스트를 위해서 text를 위에서 미리 입력하고 밑에는 주석처리함
  18. // scanf("%s",text,sizeof(text));
  19.  
  20. w = strtok(text, " "); // 단어 읽기
  21. while (w) {
  22.  
  23. // 등록 여부
  24. isRegistered = false;
  25. for (i = 0; i < totalWordCount; i++)
  26. {
  27. if (strcmp(word[i], w) == 0)
  28. {
  29. isRegistered = true;
  30. count[i]++;
  31. break;
  32. }
  33. }
  34.  
  35. if(isRegistered)
  36. {
  37. break;
  38. }
  39. else
  40. {
  41. word[totalWordCount] = w; // 등록
  42. count[totalWordCount] = 1;
  43. totalWordCount++;
  44. }
  45.  
  46. w = strtok(NULL, " "); // 다음 단어
  47. }
  48.  
  49. for (i = 0; i < totalWordCount; i++)
  50. {
  51. printf("\n%s: %d", word[i], count[i]);
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
당신이 원하는 문장을 쓰세요. 
i: 1
am: 1
groot: 2
we: 1
are: 1