简体   繁体   English

strcmp强制cmd错误

[英]strcmp forcing cmd error

I'm writing a program which the goal is to find strings which appear X or more times. 我正在编写一个程序,目标是查找出现X次或多次的字符串。

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. 我有一个单词列表,我的输入是给一个int值,我们称它为X,然后程序找到文件中出现X次或多次的单词,然后,输出为HOW MANY WORDS出现X次或多次。在文件中。 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. 首先,我正在测试将自己的文字写在cmd上的函数,但是它不起作用,我也不知道为什么。 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. 第一个if表示字符串是否相等,然后在结果中加1。第二个表示如果它们不相等,结果变为0,以便它可以再次查找有多少个相等数;第三个表示结果是否为等于或大于X,那么对howMany有更多的价值,这是我在程序结束时想要的。

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. i的值为n-1a[i+1]实际上是a[n] ,它是数组末尾的一个元素。 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 : 您需要更改循环以在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. 第三,假设您对单词进行了排序,那么当您找到一个新单词时,计数器将从0开始。 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. 因此,当您第一次找到一个单词时,计数为0,然后当您发现第二个单词时,计数为1,依此类推。 You need to start result at 1 when you find a new word. 找到新单词时,您需要从1开始result

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

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