我有两个功能我已经写出来了,但我有困难把它们结合在一起。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//prototypes:
int insert(char *word, char *Table[], int n);

int main(void)
{
    char buf[100];
    char *token;

    printf("Enter a string: ");
    fgets(buf, sizeof(buf), stdin);

    token = strtok(buf, " ,.-";

    while(token != NULL)
    {
        printf("%s\n", token);
        token = strtok(NULL, " ,.-");
    }
    return 0;
}
int insert(char *word, char *Table[], int n)
{
//*word is the string to be added, *Table is the array, n is the amount of things in array
//after insertion
    #define MAXSTRINGS 5
    #define LENSTRING 100

    int counter = 0;
    n = sizeof(Table)/sizeof(int);

    while(counter < MAXSTRINGS && strlen(Table[counter]))
    {
        if (strcmp(Table[counter], word) == 0)
        {
            return n;
            break;
        }
        else
        {
            counter++;
        }
    }
    strcpy(Table[counter], word);
    prinf("\n added at %d", counter);
    return n;
}

如何将insert函数添加到主函数中以将标记化字符串带到数组中?例如,如果我输入“dog is brown”,它将标记为“dog”、“is”、“brown”,然后我想将这些单词中的每一个都插入到表中。我不能完全把它裹住。有人能告诉我正确的方向吗?

最佳答案

假设您将在main返回后丢弃表,那么您应该在main中分配一个表结构,然后当您获得令牌时,找出它的大小,并使用您分配的表、令牌指针及其大小n调用insert函数。

关于c - C-插入现有数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33309188/

10-11 22:55
查看更多