gpt4 book ai didi

c - 如何避免同一输出出现多个印记

转载 作者:行者123 更新时间:2023-11-30 19:38:55 26 4
gpt4 key购买 nike

我在输出部分遇到问题,我发送了多少个具有相同长度的单词的计数。如果两个或多个单词具有相同的长度,我会得到有关这些单词的多个输出,而不是一个。

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

/* Write a program that captures using the gets function a string of maximum
5 words separated by spaces, for a total of up to 60 characters. The
program must:
a. Determine how many words are actually contained in the string
b. Calculate the average length of the words
c. Produce a statistic on the length of the words.

If the input string is “this string contains five words”, the
program will print on the screen:
The string contains 5 words
The average word length is 5.4 characters
The string contains
2 words of 4 characters <- at the moment this line gets printed twice for some reason
1 word of 5 characters
1 word of 6 characters
1 word of 8 characters*/


#define N 61
int main()
{
char str[N]={0};
int wc[5]={0};
int dup[5]={0};
int i=0, j=0, k=0, m=0, n=0, x, count=0, z=0, y=0;
float avg=0, sum=0;

printf("Introduce a string of [AT MOST] 5 words (60 characters): \n");
fgets(str, N, stdin);

//word count + word length
for (i=0; str[i] != '\0'; i++)
{
if (isalpha(str[i]))
{
count++;
while (isalpha(str[i]))
{
k++;
i++;
}
}
if ((isdigit(str[i])) || (ispunct(str[i])) || (isspace(str[i])) || (str[i] != '\0'))
{
wc[j]=k;
j++;
k=0;
}
}
printf("\nThere are %d words in the sentence.\n\n", count);

// checking for duplicates (same word length)
for (m=0; m<5 && wc[m]!=0; m++)
{ x=0;
sum=(sum+wc[m]);
for (n=0; n<5; n++)
{
if (wc[m]==wc[n] && m!=j)
x++;
}
dup[y]=x;
y++;
}
for (z=0; z<5 && dup[z]!=0; z++)
{
printf("%d word/s contain %d characters.\n", dup[z], wc[z]);
}

avg=sum/m;
printf("\n\nThe average length of the words in the sentence is %.2f characters.\n\n", avg);
return 0;
}

最佳答案

请参阅我已经添加了访问的 arr 并且我已经修改了第二个 for 循环。这个解决方案只是编辑你的代码来指出错误。更好的选择是对 wc 数组进行排序,然后线性扫描排序后的输出,因为重复项将放在一起。

int main()
{
char str[N]={0};
int wc[5]={0};
int dup[5]={0};
int i=0, j=0, k=0, m=0, n=0, x, count=0, z=0, y=0;
float avg=0, sum=0;
int visited[5] = {0};
printf("Introduce a string of [AT MOST] 5 words (60 characters): \n");
fgets(str, N, stdin);

//word count + word length
for (i=0; str[i] != '\0'; i++)
{
if (isalpha(str[i]))
{
count++;
while (isalpha(str[i]))
{
k++;
i++;
}
}
if ((isdigit(str[i])) || (ispunct(str[i])) || (isspace(str[i])) || (str[i] != '\0'))
{
wc[j]=k;
j++;
k=0;
}
}
printf("\nThere are %d words in the sentence.\n\n", count);

// checking for duplicates (same word length)
for (m=0; m<5 && wc[m]!=0; m++)
{ x=0;
sum=(sum+wc[m]);

if (!visited[m]) {
visited[m] = 1;

for (n=0; n<5 && wc[n]!=0; n++)
{
if (wc[m]==wc[n] && m!=j) {
if (!visited[n]) {
visited[n]=1;
}
x++;
}
}
}
dup[y]=x;
y++;
}
for (z=0; z<5; z++)
{
if (dup[z]!=0) {
printf("%d word/s contain %d characters.\n", dup[z], wc[z]);
}
}

avg=sum/m;
printf("\n\nThe average length of the words in the sentence is %.2f characters.\n\n", avg);
return 0;
}

关于c - 如何避免同一输出出现多个印记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com