c - 拆分一个字符串并打印出每个单词

标签 c

我接到了一项任务,要求用户输入一行文本。该程序在到达逗号时拆分每个单词。完成后,它只需在新行上打印出每个单词。例如:

请输入一行文字:Mary,had,a,little,lamb

Mary
had
a
little
lamb

我不能使用 strtok() 因为它不允许用于此任务。我可以使用:

printf(), fprintf(), malloc(), calloc(), realloc(), free(), fgets(), snprintf(), strncpy() >、strncat()strncmp()strdup()strlen()strchr ()

到目前为止我已经完成了:

   int i;

   char line[256];
   char *next;

   printf("Enter the string\n");
   fgets(line, 256, stdin);

   char *curr = line;

   while ((next = strchr(curr, ',')) != NULL) {
      curr = next + 1;
   }

   int len = strlen(curr);

   for (i = 0; i < len; i++) {
      printf("%c", curr[i]);
   }

那么现在如果我的字符串是:Mary,had,a,little,lamb

它只会打印出来:

羊肉

有什么建议吗?

最佳答案

很遗憾他们没有列出最适合这项任务的函数:strcspn()。您可以按照张贴的方式使用 strchr(),但打印结果时出现问题。您可以使用 %.*s 格式说明符打印子字符串:

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

int main(void) {
    char line[256];

    printf("Enter the string\n");
    if (fgets(line, 256, stdin)) {
        char *curr = line;
        char *next;

        while ((next = strchr(curr, ',')) != NULL) {
            /* found a separator: print the word between curr and next */
            printf("%.*s\n", (int)(next - curr), curr);
            curr = next + 1;
        }
        /* print the last word */
        printf("%s", curr);
    }
    return 0;
}

如果你不能使用这个有点高级的 printf 特性,你可以这样修改字符串:

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

int main(void) {
    char line[256];

    printf("Enter the string\n");
    if (fgets(line, 256, stdin)) {
        char *curr = line;
        char *next;

        while ((next = strchr(curr, ',')) != NULL) {
            /* found a separator: replace it with '\0' */
            *next++ = '\0';
            printf("%s\n", curr);
            curr = next;
        }
        /* print the last word */
        printf("%s", curr);
    }
    return 0;
}

另请注意,您可以使用更简单的解决方案,甚至没有缓冲区,但使用 getchar()putchar(),它们不在您的列表中:

#include <stdio.h>

int main(void) {
    int c;

    while ((c = getchar()) != EOF) {
        putchar(c == ',' ? '\n' : c);
        if (c == '\n')
            break;
    }
    return 0;
}

编辑:如果相邻的 2 个逗号表示单词的字面逗号部分,请尝试以下操作:

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

int main(void) {
    char line[256];

    printf("Enter the string\n");
    if (fgets(line, 256, stdin)) {
        char *curr = line;
        char *next;

        while ((next = strchr(curr, ',')) != NULL) {
            /* found a separator: print the word between curr and next */
            if (next[1] == ',') {
                next++;
                printf("%.*s", (int)(next - curr), curr);
            } else {
                printf("%.*s\n", (int)(next - curr), curr);
            }
            curr = next + 1;
        }
        /* print the last word */
        printf("%s", curr);
    }
    return 0;
}

EDIT 2 鉴于您应该将字符串拆分为指针数组,代码稍微复杂一些。不需要分配数组,因为单词的数量受输入缓冲区大小的限制:

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

int main(void) {
    char line[256];
    char *array[256];
    int i, n;

    printf("Enter the string\n");
    if (fgets(line, 256, stdin)) {
        char *p = strchr(line, '\n');
        if (p != NULL) {
            *p = '\0';   /* strip the newline if any */
        }
        /* split the line into an array of words */
        n = 0;
        array[n++] = p = line;
        while ((p = strchr(p, ',')) != NULL) {
            *p++ = '\0';
            array[n++] = p;
        }
        /* print the words */
        for (i = 0; i < n; i++) {
            printf("%s\n", array[i]);
        }
    }
    return 0;
}

关于c - 拆分一个字符串并打印出每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42462287/

相关文章:

c - 使用内核模块与 Beagleboard xM 进行 16*2 LCD 连接

c - 在句子中存储指向单词的指针

c - 插入排序 : order numbers over 2^32

c - 使用 DFS 算法在迷宫中为汽车寻找出路(C 编程)

c - 我应该引用哪些文件来进行 Contiki 中的模拟

c++ - Pro*C 传递参数数组

c++ - 将 C++ 添加到复杂的 C 生成文件

C 命令行密码

c - 位编程

c - C 中结构体数组初始化为 0,警告 : missing braces around initializer