【问题标题】:C Resetting data counters in FOR loopC 在 FOR 循环中重置数据计数器
【发布时间】:2014-03-26 19:12:32
【问题描述】:

我有一个非常大的文本文件,我正在尝试对其进行文字分析。在字数统计中,我可能也在寻找其他信息,但为简单起见,我将其省略了。 在这个文本文件中,我有由星号“*”分隔的文本块。我在下面的代码扫描文本文件并打印出字符和单词的数量,但我想在遇到星号后重置计数器,并将所有信息存储在某种表格中。我不太担心如何制作表格,因为我不确定如何为星号之间的每个文本块循环相同的计数代码。

可能是一个 for 循环

for (arr = strstr(arr, "*"); arr; arr = strstr(arr + strlen("*"), "*"))  

示例文本文件:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-
I have a sentence. I have two sentences now.
*
I have another sentence. And another.
*
I'd like to count the amount of words and characters from the asterisk above this 
one until the next asterkisk, not including the count from the last one.
*
...
    ...
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    (EOF)

Desired output:

    *#      #words     #alphaChar
    ----------------------------
    1        9           34  
    -----------------------------
    2        5           30
    -----------------------------
    3       28           124
    ...
    ...


I have tried

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

        int main()
          {
          int characterCount=0;
          int counterPosition, wordCount=0, alphaCount=0;

          //input file
          FILE *file= fopen("test.txt", "r");
          if (file== NULL)
            printf("Cannot find the file.\n");


          //Count total number of characters in file
          while (1)
              {
              counterPosition = fgetc(speechFile);
              if (counterPosition == EOF)
                break;
              ++characterCount;
              }

          rewind(file); // Sends the pointer to the beginning of the file

          //Dynamically allocate since array size cant be variable
          char *arr= ( char*) malloc(totalCharacterCount);

          while(fscanf(speechFile, "%c", &arr[i]) != EOF ) //Scan until the end of file.
            i++;   //increment, storing each character in a unique position



              for(i = 0; i <characterCount; i++)
                  {
                  if(arr[i] == ' ') //count words
                    wordCount++;

                  if(isalpha(arr[i]))  //count letters only
                    alphaCount++;

                  }//end for loop

              printf("word count is %d and alpha count is %d", wordCount,alphaCount);
          }

【问题讨论】:

  • wordcount 和 alphacount 未初始化。您还增加了每个空格的字数,因此“”(2 个空格)将计为两个单词。 char *arr= ( char*) malloc(totalCharacterCount*sizeof(int)); 可以是 char *arr= malloc(totalCharacterCount);
  • 添加了修复。不过,这不是问题。我重新编写了大部分代码,而不是复制和粘贴,所以我忘记了初始化
  • 为什么程序在只需要一次的情况下进行三遍(文件两次,数组一次)。另外:该计划似乎与您的目标几乎没有关系。
  • 三遍是什么意思?我是新手。此外,该计划与我的目标非常相关。我将自己“绘制”图表。我只是问如何在每组星号之间重置一个计数器。

标签: c loops for-loop counter reset


【解决方案1】:

由于数组 arr[] 中有完整的文件文本,因此您需要使用 * 作为分隔符来分割该字符串 arr。您可以使用strtok() 来划分该字符串,使用* 作为分隔符。然后对每个token进行字数和字符数操作。阅读此链接以了解strtok。

【讨论】:

  • 我记得有人在另一篇文章中提到了 strtok,但出于某种原因,我认为这无关紧要。你说的有道理,我试试看。谢谢!
  • @user3465668 好的。你有问题讨论在stackoverflow中使用strtok()所以我努力添加代码。阅读其中之一。查看this 了解如何使用 strtok。它可以帮助你编码
猜你喜欢
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
  • 2013-11-06
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
相关资源
最近更新 更多