如何避免同一输出的多个标记

时间:2016-05-23 17:55:47

标签: c printf

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

#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;
}

1 个答案:

答案 0 :(得分: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;
}