简体   繁体   中英

strcmp forcing cmd error

I'm writing a program which the goal is to find strings which appear X or more times.

I have a list of words and my input is to give an int value, let's call it X, then the program finds the words that appear X times or more in the file, then, the output is HOW MANY WORDS appear X or more times in the file. For a start I'm testing the function writting myself the words on the cmd and it's not working and I don't know why. The program just stops working and closes.

char *str_dup(const char *s){

    char *result = (char *) malloc(strlen(s) + 1);
    strcpy(result, s);
    return result;
}

int str_readline(FILE *f, char *s){

  int result = EOF;

  char *p = fgets(s, INT_MAX, f);

  if (p != NULL){


    result = (int) strlen(s);
    if (result > 0 && s[result-1] == '\n')
      s[--result] = '\0';
  }

  return result;
}

int strings_read(FILE *f, char **a){
    int result = 0;
    char line[10000];
    while(str_readline(f, line) !=EOF){
        a[result++] = str_dup(line);
    }
    return result;
}

int strings_get(char **a){
    return strings_read(stdin, a);
}

int howMany(char **a, int n, int x){
    int result = 0;
    int howMany = 0;
    for(int i=0; i<n; i++){
        if(strcmp(a[i], a[i+1]) == 0){
            result++;
        }
        else if(strcmp(a[i], a[i+1]) > 0 || strcmp(a[i], a[i+1]) < 0){
            result = 0;
        }
        if(result >= x){
            howMany++;
        }

    }
    return howMany;
}

void test_howMany(void){
  char *a[1000];
  int x;
  scanf("%d", &x);
  int n;
  while((n = strings_get(a)) != 0){
    int z = howMany(a, n, x);
    printf("%d\n", z);
  }
}

int main(void){
    test_howMany();
    return 0;
}

The FIRST if means if the strings are equal, then add 1 value to result The SECOND one means if they are not equal, result becomes 0 so that it can start looking again for how many equals we have the THIRD one means if the result is equal or above X then we have 1 more value on howMany, which is what I want at the end of the program.

What is wrong?

There are three main problems here.

First, you're reading past the end of the array:

for(int i=0; i<n; i++){
    if(strcmp(a[i], a[i+1]) == 0){

When i has a value of n-1 , a[i+1] is actually a[n] , which is one element past the end of the array. Reading past the end of an array invokes undefined behavior which in this caes manifests in a crash. You need to change your loop to stop at n-1 :

for(int i=0; i<n-1; i++){

Second, your algorithm is only comparing adjacent words in this list. For this to work, your list needs to be sorted, and your code isn't doing that.

Third, assuming you did sort the words, your counter starts at 0 when you find a new word. So when you first find a word the count is 0, then when you find a second occurrence the count is 1, and so forth. You need to start result at 1 when you find a new word.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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