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

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

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".

#include <stdio.h>
#include <string.h>

void main(void)
{
    char finalArr[100];
    char src;
    int i = 0;

    printf("Enter a string: ");
    // fill the array
    while ((src = getchar()) != '\n' && i < 99)
    {
        finalArr[i] = src;
        i++;
    }
    finalArr[i] = '
#include <stdio.h>
#include <string.h>
void main(void)
{
char finalArr[100];
char src;
int i = 0;
printf("Enter a string: ");
// fill the array
while ((src = getchar()) != '\n' && i < 99)
{
finalArr[i] = src;
i++;
}
finalArr[i] = '\0';
printf("Result is: %s\n", finalArr); // output array
// writing first word to distArr[]
char distArr[100];
int j = 0;
for (j = 0; j < strlen(finalArr); j++)
{
distArr[j] = finalArr[j];
if (finalArr[j] == ' ')
{
break;
}
}
distArr[j] = '\0';
printf("Dist array: %s\n", distArr);
printf("%c", distArr[0]);
// Compare the first word with all elements of the array finalArr[]
int count = 0;
int d;
for (int k = 0; k < strlen(finalArr); k++) {
if (finalArr[k] == ' ') {
k++;
}
for(int d = k; d < strlen(distArr); d++) {
if (distArr[d] == finalArr[d]) {
count++;
}
}
}
printf("Count: %d", count);
}
'; printf("Result is: %s\n", finalArr); // output array // writing first word to distArr[] char distArr[100]; int j = 0; for (j = 0; j < strlen(finalArr); j++) { distArr[j] = finalArr[j]; if (finalArr[j] == ' ') { break; } } distArr[j] = '
#include <stdio.h>
#include <string.h>
void main(void)
{
char finalArr[100];
char src;
int i = 0;
printf("Enter a string: ");
// fill the array
while ((src = getchar()) != '\n' && i < 99)
{
finalArr[i] = src;
i++;
}
finalArr[i] = '\0';
printf("Result is: %s\n", finalArr); // output array
// writing first word to distArr[]
char distArr[100];
int j = 0;
for (j = 0; j < strlen(finalArr); j++)
{
distArr[j] = finalArr[j];
if (finalArr[j] == ' ')
{
break;
}
}
distArr[j] = '\0';
printf("Dist array: %s\n", distArr);
printf("%c", distArr[0]);
// Compare the first word with all elements of the array finalArr[]
int count = 0;
int d;
for (int k = 0; k < strlen(finalArr); k++) {
if (finalArr[k] == ' ') {
k++;
}
for(int d = k; d < strlen(distArr); d++) {
if (distArr[d] == finalArr[d]) {
count++;
}
}
}
printf("Count: %d", count);
}
'; printf("Dist array: %s\n", distArr); printf("%c", distArr[0]); // Compare the first word with all elements of the array finalArr[] int count = 0; int d; for (int k = 0; k < strlen(finalArr); k++) { if (finalArr[k] == ' ') { k++; } for(int d = k; d < strlen(distArr); d++) { if (distArr[d] == finalArr[d]) { count++; } } } printf("Count: %d", count); }

preferably, use one-dimensional arrays without strtok

答案1

得分: 0

这样的代码可能会起作用:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        const char delim[] = " ";
        char finalArr[] = "qwe qwe qwe";
        char distArr[] = "qwe";
        int count = 0;
        
        char *ptr = strtok(finalArr, delim);
    
    	while(ptr != NULL)
    	{
    		if (!strcmp(ptr, distArr))
    		    count++; //strings match
    		ptr = strtok(NULL, delim);
    	}
    	
    	printf("Result:%d", count);
    
        return 0;
    }
英文:

Something like this could work:

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main() {
    const char delim [] = &quot; &quot;;
    char finalArr [] = &quot;qwe qwe qwe&quot;;
    char distArr [] = &quot;qwe&quot;;
    int count = 0;
    
    char *ptr = strtok(finalArr, delim);

	while(ptr != NULL)
	{
		if (!strcmp(ptr,distArr))
		    count++; //strings match
		ptr = strtok(NULL, delim);
	}
	
	printf(&quot;Result:%d&quot;,count);

    return 0;
}

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:

确定