字符串中单词的出现频率

时间:2018-12-29 15:12:23

标签: c string

好的,我正在解决我的作业,在这个特定程序中,单词“ say”的输出即使显示两次也显示为1。有人可以帮我吗?

[i if i in x else i/2 if i in z else i/3 for i in y] 

以下是输出:

//Start
    # include <stdio.h>
    # include <string.h>
    int main()
    {
     char Str[100]="Martha! Why did you say that name? Please! Stop! Why did 
                    you say that name?", Words[100][100], Temp[100];
  int i, j, k, n, Count;
  j=k=0;
  //Accepting input
  //gets(Str);
  Str[strlen(Str)]='\0';
  //Copying Each and every word into a 2-D Array from the string
   for(i=0;Str[i]!='\0';i++)
    {
     if(Str[i]==' ')
      {
       Words[j][k]='\0';
       k=0;
       j++;
      }
     else
      {
       Words[j][k++]=Str[i];
      }
    }
  Words[j][k] = '\0'; //Null character for last word
  n=j;
  //Sorting the array of words
  for(i=0;i<n-1;i++)
   {
    for(j=i+1;j<n;j++)
     {
      if(strcmp(Words[i], Words[j])>0)
       {
         strcpy(Temp, Words[i]);
         strcpy(Words[i], Words[j]);
         strcpy(Words[j], Temp);
       }
     }
   }
  printf("\n");
  //Displaying frequecncy of each word

   for(i=0;i<n;i+=Count) //Incrementing by count to process the next word
    {
     Count=1;
       {
        for(j=i+1;j<=n;j++)
          {
           if(strcmp(Words[i], Words[j])==0)
            {
             Count++;
            }
          }
       }
      printf("%s\t%d\n", Words[i], Count); //Count is used to display the frequecncy of each word
    }
  printf("\n");
  return 0;
}//End

如您所见,即使在字符串中两次出现“ say”一词,其输出也会显示其频率。 Check the link for the output on the terminal?

1 个答案:

答案 0 :(得分:0)

class SelfSizingTableView: UITableView { override var intrinsicContentSize: CGSize { return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height) } override var contentSize: CGSize { didSet { invalidateIntrinsicContentSize() } } } 设置不正确。

对于n,从概念上讲,这应该是n = j;n = j+1;代表n的单词而不是最后一个索引。

n

在没有上述更改的情况下,由于// n = j; n = j+1; 被视为字数计数,因此数组未完全排序,但是它太小了1-

排序不正确的数组是

n

排序不完整,现在使用Martha!, Please!, Stop!, Why, Why, did, did, name?, say, say, that, that, you, you, name? 作为最后一个索引,因为两次发现n却发现频率计数混乱,但没有连续的顺序-因此跳过了第一个"name?"

"say"

已修复的代码输出

// for (j = i + 1; j <= n; j++) {
for (j = i + 1; j < n; j++) {