-5

Snapshot

Introduction

Usual search engines receive a set of keywords and look for all the documents that contain these keywords. The documents are listed in the order of document significance. In this problem we consider the significance of a document for a set of keywords is given by the minimum number of words of the continuous piece of text that contains all the searched keywords. For instance: consider the keywords “2008” and “IEEEXtreme”, and the following two texts: “The registration for the 2008 edition of IEEEXtreme is now open” and “IEEEXtreme 2008 edition is going to take place on March 8th 2008”. The significance of the first text is 4, and of the second one is 2. If any of the given words is not present in the text, the significance is zero.

Task

Please write a program that reads from the standard input a text in which the words are separated only by spaces, and finds the significance of text against the keywords given as the parameters to your program.

Syntax

For the input text:

The registration for the 2008 edition of IEEEXtreme is now open

your program executed as:

> snapshot 2008 IEEEXtreme

should write 4 on the standard output. Note: if not all the words are found, the program should return 0.

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

int compare(char *x, char *z) {
  int a = 0;
  if (strlen(x) == strlen(z)) {
    while (a < strlen(x)) {
      if (x[a] == z[a])
        a++;
      else
        return 0;
    }
    return 1;
  }
}

int verify(int q, int n, char *v) {
  static int flag2 = 0;
  static int error = 0;

  if ((v[0] == '#') && (v[1] == '#') && (v[2] == '#') && (v[3] == '#')
      && (v[4] == '#')) {
    flag2 = 1;
  } else {
    error++;
  }

  if ((q = n - 1) && flag2 == 1 && error == 0)
    return 1;
  else
    return 0;
}

int main(int argc, char *argv[]) {

  char text[1000];
  char word[30];

  FILE *fp = fopen("filename", "r");

  int i = 0, j = 0, k = 0, y = 1, w = 1, t = 1, flag = 0, signifiancia = 0,
      sucesso = 0;

  while (feof(fp))
    text[i++] = fgetc(fp);

  text[i] = '\0';

  while (text[j] != '\0') {
    if (text[j] == ' ') {
      j++;
      word[k] = '\0';
      k = 0;
      while (y < argc) {
        compare(argv[y], word);
        if (1) {
          flag = 1;
          argv[y] = "#####";
          signifiancia++;
          y++;
        } else {
          if (flag = 1)
            signifiancia++;
          y++;
        }
      }
    } else {
      word[k] = text[j];
      j++;
      k++;
    }
    while (w < argc) {
      verify(t, argc, argv[w]);
      t++;
      if (1) {
        sucesso++;
        printf("%d", signifiancia);
      }
    }
  }
  if (sucesso == 0)
    printf("0");
}

The error given is: Segmentation fault (core dumped)

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 2
    Strap this into your debugger and find out more about the crash. A segmentation fault is almost always accessing an invalid pointer, or using an array out of bounds. – tadman Jan 16 '21 at 15:54
  • Tip: Get out of the habit of declaring your variables in a giant jumble. Declare them *where they are used* and as close as possible to their first usage. – tadman Jan 16 '21 at 15:56
  • while(feof(fp)) is always **doubly** wrong. You probably meant `while (!feof(fp))` which would just be always **singly** wrong :D – Antti Haapala -- Слава Україні Jan 16 '21 at 15:56
  • The problem is actually very simple: you're throwing **all the code** you could imagine at once into the file and then it doesn't work. Work in small increments and verify that they do indeed work as intended. – Antti Haapala -- Слава Україні Jan 16 '21 at 15:58
  • Hm. You never store or check the return values of `compare` and `verify`. The `if (1) ...` after these calls does not mean "if `verify`/`compare` returned 1", it means "always do this", because 1 as a condition is considered true. – M Oehm Jan 16 '21 at 16:10

1 Answers1

2

At least these problems:

Missing return

When if (strlen(x) == strlen(z)) is false, function does not return anything.

Yet calling code does not use the return value anyways.

Assignment rather than compare

     if (flag = 1)

Too many loops

Code iterates once too often.

while (feof(fp))
  text[i++] = fgetc(fp);

Infinite loop

Once while (w < argc) { loop is entered, it appears to iterate infinitely - likely leading to UB.

Failure to prevent buffer over-runs

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256