c - 在字符串中查找动词

标签 c pointers user-defined-functions binary-search

我正在尝试(并且遇到了麻烦)编写一个程序(在 C 中),该程序在命令行中接受一个字符串(例如 $ test.out“This is a string”)并通过该字符串查找动词(和名词,但如果我想出动词,我可以自己做名词)。

文件 lexicon.h 中给出了按字母顺序排序的动词列表,这就是我应该用作字典的内容。

我知道如何从命令行接受字符串并使用该输入创建一个字符串数组,每个字符串本身都是一个单独的词,我已经有一个可以执行此操作的工作程序,我希望将一部分用于此。

我应该创建一个名为 binary_search(...stuffgoeshere...) 的函数,并使用它来搜索词典文件并找到动词。

我想要一些关于如何创建一个函数 (binary_search) 的建议或指导,该函数可以检查一个已经分离的单词是否与 lexicon.h 中列表中的任何单词匹配。我不希望有人只写一个答案,我想知道你为什么建议你做什么。希望我能从中学到一些有趣的东西!

我知道这很乱,但这就是我目前所拥有的。

另请注意,词典的动词数组有 637 个值(如我设置 int size = 637 时所见) 该程序不再编译,因为我还没有弄清楚如何使 binary_search 函数工作。我正在尝试修改类示例中使用的二进制搜索函数,但是,该函数对文本文件中的数字进行排序,而不是字符串。

如果还有什么我应该包括的,请告诉我。感谢您的帮助!

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

    int binary_search(char word[], char verbs[][], int size);

    int
    main(int argc, char*argv[])
    {
            char word[80];
            char str[80],
                 args[80][80];
            int counter = 0,
                a = 0,
                i = 0,
                index = 0,
                t = 0;
            while(str[a] != '\0')
            {
                    if(str[a] == ' ')
                    {
                            args[index][i] = '\0';
                            i = 0;
                            a++;
                            index ++;
                            counter ++;
                    }
                    args[index][i++] = str[a++];
            }
            args[index][i] = '\0';
            counter = counter + 1;
            printf("\nThe verbs were: ");
            int verbposition= -1;
         int size = 637;
            while(t<counter)
            {
                    strcpy(word, args[t]);
                    verbposition = binary_search(word, verbs, size);

                    if(verbposition > -1)
                            printf("%s", args[t]);

                    t++;

            }

            return 0;
    }

    int
    binary_search(char word[], char &verbs[][], int size)
    {
            int bottom = 0,
      top = size - 1,
                found = 0,
                middle;
            while(bottom <= top && !found)
            {
                    middle = (bottom + top) / 2;

                    if(strcmp(word, verbs[middle]))
                    {
                            found = 1;
                            return = middle;
                    }
                    if(strcmp(word, verbs[middle]) > 0)
                    {
                            top = middle - 1;
                    }
                    else
                            bottom = middle + 1;   
     }
            return -1;
    }

最佳答案

您走在正确的轨道上。我强烈建议您使用 print 语句,因为您会清楚地知道哪里出错了。

关于c - 在字符串中查找动词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16743331/

相关文章:

c - 当 memcpy 应该足够时,为什么 wmemcpy 存在?

c - 在 C 中释放链表

c - 用指针调用函数不会更改该指针的值吗?

c++ - 我可以在 C++ 程序之外使用随机内存地址访问随机数据吗

c - 在弹性规则中使用外部定义的常量

c++ - 什么是 GCC 默认包含目录?

c++ - 为什么编译器不能区分 double** 和 double*

scala - 将两个Array [string]类型的spark sql列合并到新的Array [string]列中

vba - Excel UDF 加权 RANDBETWEEN()

python - 将BLOB传递给用户定义函数的Python Sqlite3给出None