在一个数组中如何找到相同的单词?

huangapple go评论106阅读模式
英文:

How can I find the same words in an array?

问题

我有一个需要统计数组 finalArr[] 中第一个单词数量的程序。为此,我将第一个单词分别写入数组 distArr[]。然后,我将 finalArr[] 数组的元素与 distArr[] 数组的元素进行比较。但最终结果是不正确的。例如,在 finalArr[] 中我有字符串 "qwe qwe qwe",正确的结果应该是 "3"。

英文:

I have a program that needs to count the number of first words in an array finalArr[].
To do this, I separately write the first word to an array distArr[]. Then I compare the elements of the finalArr[] array with the elements of the distArr[] array. But final result is incorrect. For example, I have string "qwe qwe qwe" in finalArr[], and correct result must be "3".

  1. #include <stdio.h>
  2. #include <string.h>
  3. void main(void)
  4. {
  5. char finalArr[100];
  6. char src;
  7. int i = 0;
  8. printf("Enter a string: ");
  9. // fill the array
  10. while ((src = getchar()) != '\n' && i < 99)
  11. {
  12. finalArr[i] = src;
  13. i++;
  14. }
  15. finalArr[i] = '
    #include <stdio.h>
  16. #include <string.h>
  17. void main(void)
  18. {
  19. char finalArr[100];
  20. char src;
  21. int i = 0;
  22. printf("Enter a string: ");
  23. // fill the array
  24. while ((src = getchar()) != '\n' && i < 99)
  25. {
  26. finalArr[i] = src;
  27. i++;
  28. }
  29. finalArr[i] = '\0';
  30. printf("Result is: %s\n", finalArr); // output array
  31. // writing first word to distArr[]
  32. char distArr[100];
  33. int j = 0;
  34. for (j = 0; j < strlen(finalArr); j++)
  35. {
  36. distArr[j] = finalArr[j];
  37. if (finalArr[j] == ' ')
  38. {
  39. break;
  40. }
  41. }
  42. distArr[j] = '\0';
  43. printf("Dist array: %s\n", distArr);
  44. printf("%c", distArr[0]);
  45. // Compare the first word with all elements of the array finalArr[]
  46. int count = 0;
  47. int d;
  48. for (int k = 0; k < strlen(finalArr); k++) {
  49. if (finalArr[k] == ' ') {
  50. k++;
  51. }
  52. for(int d = k; d < strlen(distArr); d++) {
  53. if (distArr[d] == finalArr[d]) {
  54. count++;
  55. }
  56. }
  57. }
  58. printf("Count: %d", count);
  59. }
  60. ';
  61. printf("Result is: %s\n", finalArr); // output array
  62. // writing first word to distArr[]
  63. char distArr[100];
  64. int j = 0;
  65. for (j = 0; j < strlen(finalArr); j++)
  66. {
  67. distArr[j] = finalArr[j];
  68. if (finalArr[j] == ' ')
  69. {
  70. break;
  71. }
  72. }
  73. distArr[j] = '
    #include <stdio.h>
  74. #include <string.h>
  75. void main(void)
  76. {
  77. char finalArr[100];
  78. char src;
  79. int i = 0;
  80. printf("Enter a string: ");
  81. // fill the array
  82. while ((src = getchar()) != '\n' && i < 99)
  83. {
  84. finalArr[i] = src;
  85. i++;
  86. }
  87. finalArr[i] = '\0';
  88. printf("Result is: %s\n", finalArr); // output array
  89. // writing first word to distArr[]
  90. char distArr[100];
  91. int j = 0;
  92. for (j = 0; j < strlen(finalArr); j++)
  93. {
  94. distArr[j] = finalArr[j];
  95. if (finalArr[j] == ' ')
  96. {
  97. break;
  98. }
  99. }
  100. distArr[j] = '\0';
  101. printf("Dist array: %s\n", distArr);
  102. printf("%c", distArr[0]);
  103. // Compare the first word with all elements of the array finalArr[]
  104. int count = 0;
  105. int d;
  106. for (int k = 0; k < strlen(finalArr); k++) {
  107. if (finalArr[k] == ' ') {
  108. k++;
  109. }
  110. for(int d = k; d < strlen(distArr); d++) {
  111. if (distArr[d] == finalArr[d]) {
  112. count++;
  113. }
  114. }
  115. }
  116. printf("Count: %d", count);
  117. }
  118. ';
  119. printf("Dist array: %s\n", distArr);
  120. printf("%c", distArr[0]);
  121. // Compare the first word with all elements of the array finalArr[]
  122. int count = 0;
  123. int d;
  124. for (int k = 0; k < strlen(finalArr); k++) {
  125. if (finalArr[k] == ' ') {
  126. k++;
  127. }
  128. for(int d = k; d < strlen(distArr); d++) {
  129. if (distArr[d] == finalArr[d]) {
  130. count++;
  131. }
  132. }
  133. }
  134. printf("Count: %d", count);
  135. }

preferably, use one-dimensional arrays without strtok

答案1

得分: 0

  1. 这样的代码可能会起作用:
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main() {
  5. const char delim[] = " ";
  6. char finalArr[] = "qwe qwe qwe";
  7. char distArr[] = "qwe";
  8. int count = 0;
  9. char *ptr = strtok(finalArr, delim);
  10. while(ptr != NULL)
  11. {
  12. if (!strcmp(ptr, distArr))
  13. count++; //strings match
  14. ptr = strtok(NULL, delim);
  15. }
  16. printf("Result:%d", count);
  17. return 0;
  18. }
英文:

Something like this could work:

  1. #include &lt;stdio.h&gt;
  2. #include &lt;string.h&gt;
  3. int main() {
  4. const char delim [] = &quot; &quot;;
  5. char finalArr [] = &quot;qwe qwe qwe&quot;;
  6. char distArr [] = &quot;qwe&quot;;
  7. int count = 0;
  8. char *ptr = strtok(finalArr, delim);
  9. while(ptr != NULL)
  10. {
  11. if (!strcmp(ptr,distArr))
  12. count++; //strings match
  13. ptr = strtok(NULL, delim);
  14. }
  15. printf(&quot;Result:%d&quot;,count);
  16. return 0;
  17. }

This outputs:
Result:3

huangapple
  • 本文由 发表于 2023年4月13日 22:01:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定