如何在 C 中将 2 个字符串提取到单独的数组中

How to extract 2 strings into indivitual arrays in C

该程序的目标是将 redact 字符串中的给定单词替换为完整句子中的星号。

例如给出一个完整的句子:“敏捷的棕色狐狸跳过懒惰的狗” 并编辑字符串:“the, jumps, lazy”,输出为“*** quick brown fox ***** over *** **** dog”,需要保存在 result.txt .

我的主要想法是先将密文字符串和完整的句子提取到数组中,然后再搜索单词。如果匹配,则完整句子的数组将替换为星号。最后,数组被转换回字符串。

我的代码的问题是我想从 fullSentence[] 和 redactString[] 中提取字符串到单独的数组中,但是当我 运行 这段代码输出时...

跳跃, 懒惰的 这 Y├╠╠╠╠╠╠j

这是代码:

int main(void) {

    char fullSentence[] = "The quick brown fox jumps over the lazy dog";
    int t = 0;
    int i = 0;
    char **redactArray = NULL;
    char *token = strtok(fullSentence, " ");


    char redactString[] = "the, jumps, lazy";
    int j = 0;
    char *p = strtok (redactString, ", ");
    char *extractString[3];


    for (t = 1; token; ++t) { // Extract fullSentence[]
        redactArray = realloc(redactArray, t *sizeof(*redactArray));       
        redactArray[t - 1] = malloc(strlen(token) + 1);
        strncpy(redactArray[t - 1], token, strlen(token) + 1);
        token = strtok(NULL, " ");
    }

    for (i = 0; i < t-1; ++i){
        printf("%s\n", redactArray[i]);
    }


    while (p != NULL) { // Extract redactString[]
        extractString[j++] = p;
        p = strtok(NULL, ", ");
    }

    for (j = 0; j < sizeof(extractString); ++j) {
        printf("%s\n", extractString[j]);
    }

    return extractString;
}

提前致谢:)

请您尝试以下操作:

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

int main(void) {
    char fullSentence[] = "The quick brown fox jumps over the lazy dog";
    char **fullArray = NULL;    // tokenized array for fullSentence
    int m = 0;                  // array length of fullArray

    char redactString[] = "the, jumps, lazy";
    char **redactArray = NULL;  // tokenized array for redactString
    int n = 0;                  // array length of redactArray

    char *token;
    int i, j, k;

    for (token = strtok(fullSentence, " "); token != NULL; token = strtok(NULL, " ")) {
        fullArray = realloc(fullArray, (m + 1) * sizeof(*fullArray));
        fullArray[m] = malloc(strlen(token) + 1);
        strncpy(fullArray[m], token, strlen(token) + 1);
        m++;
    }

    for (token = strtok(redactString, ", "); token != NULL; token = strtok(NULL, ", ")) {
        redactArray = realloc(redactArray, (n + 1) * sizeof(*redactArray));
        redactArray[n] = malloc(strlen(token) + 1);
        strncpy(redactArray[n], token, strlen(token) + 1);
        n++;
    }

    // compare the words one by one ignoring case
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            if (strcasecmp(fullArray[i], redactArray[j]) == 0) {
                // the words match. redact the word in fullArray[i]
                for (k = 0; k < strlen(fullArray[i]); k++) {
                    fullArray[i][k] = '*';
                }
            }
        }
    }

    // print the redacted string
    for (i = 0; i < m; i++) {
        printf("%s%s", fullArray[i], i == m - 1 ? "\n" : " ");
    }

    return 0;
}

输出:

*** quick brown fox ***** over *** **** dog