简体   繁体   English

使用fgets和strstr在C中查找和计数字符串

[英]Finding and counting strings in C using fgets and strstr

I'm trying to work on an assignment. 我正在尝试一项任务。 The idea is to get an array of strings, and a file stream. 这个想法是获得一个字符串数组和一个文件流。 I need to look for those strings in the file, and count the occurrence of these strings. 我需要在文件中查找那些字符串,并计算这些字符串的出现次数。

I think I've got the basic loop down. 我想我的基本循环了。 The only problem is that, when I find a string in the line, I want to search for that string again (in case of more than one occurrence) by starting from 1 + the position where the found string starts. 唯一的问题是,当我在行中找到一个字符串时,我想从1 +找到的字符串开始的位置开始再次搜索该字符串(如果出现多次)。

#define LINE_MAX_CHARS 1000

// n = number of strings to be found
// **strings = array of strings to look for in file
void count_occurrences (int n, FILE *file, char **strings) {
  char str[LINE_MAX_CHARS]; // buffer
  int count = 0;
  while (fgets(str, LINE_MAX_CHARS, file) !=  NULL){ // for each line
      for (int i = 0; i < n; i++){ // for each word in line
          char *found; 
          found = (strstr(str, (*(strings + i)))); // search line
          if (found != NULL){ // if word found in line
            // here, I want str (the buffer) to have its 0-th element to be the element at (found + 1), 
            // and the 1-st element to be (found + 2) and so on...
            i--; // to look for same word in the rest of the line
            count = count + 1;
          }
      }
  }
}

Another problem is that I have no way of testing my code. 另一个问题是我无法测试我的代码。 I'm just given a test program that runs and tells me whether or not my code is producing the correct output. 我只是得到了一个运行的测试程序,告诉我我的代码是否产生正确的输出。

I am REQUIRED to use fgets and strstr. 我需要使用fgets和strstr。

Suggestions? 有什么建议吗?

strstr(str, strings[i]) Returns a pointer to a position in the string. strstr(str, strings[i])返回一个指向字符串中某个位置的指针。 You should be able to increment that pointer ( str++ ) and pass it straight back into strstr() in a loop, incrementing count each time, finishing the loop if strstr() returns NULL or str hits the null character. 您应该能够递增该指针( str++ )并在循环中将其直接传递回strstr() ,每次递增计数,如果strstr()返回NULLstr击空字符,则结束循环。

It should look something like this. 它看起来应该像这样。 I've not tested this; 我还没有测试过 but since this is your homework, if it doesn't work/compile quite right, I'm going to leave it to you to debug. 但是,由于这是您的作业,因此如果无法正常工作/编译,我将把它留给您进行调试。 That means I won't have done all the work for you... 这意味着我不会为您完成所有工作...

;-) ;-)

void count_occurrences (int n, FILE *file, char **strings) {
  char str[LINE_MAX_CHARS];
  int count = 0;

  while (fgets(str, LINE_MAX_CHARS, file) !=  NULL){
    for (int i = 0; i < n; i++){
      char *pos = str;

      while(((pos = strstr(pos, strings[i]) != NULL) && *pos != '\n') {
        count++;
        pos++;
      }
    }
  }
}

To count every occurrence of strings[i] in the current line, you have to use a loop and you have to let strstr start at least one position after the last occurrence. 要计算当前行中strings[i]每一次出现,您必须使用循环,并且必须让strstr在最后一次出现之后至少开始一个位置。 See the following code: 请参见以下代码:

#define LINE_MAX_CHARS 1000

// n = number of strings to be found
// **strings = array of strings to look for in file
void count_occurrences (int n, FILE *file, char **strings) {
   char str[LINE_MAX_CHARS]; // buffer
   int count = 0;
   while (fgets(str, LINE_MAX_CHARS, file) !=  NULL){ // for each line
      for (int i = 0; i < n; i++){ // for each word in line
          char *found = str;
          do { 
            found = strstr(found, strings[i]); // search line
            if (found != NULL){ // if word found in line
              count = count + 1;
              found++;
            }
          }
          while (found)
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM