【问题标题】:dividing the character in a char array based on the space [duplicate]根据空格划分字符数组中的字符[重复]
【发布时间】:2015-02-23 04:36:07
【问题描述】:

在我下面的代码中:

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



int main() {


    char text[256];

// three character pointers
    char *new_line;

// store new_line pointer with hello
    new_line = strcpy(text, "hello how are you");

    while(*new_line)
        printf("%c",*new_line++);



    return (0);
}

我正在尝试根据存储在数组中的单词之间的空间来划分 *new_line 指向的数组。更具体地说,我想为数组中的每个单词分配索引并将其显示为:

1- hello
2- how
3- are
4- you

如何打破这个 char 数组并单独获取这些单词?

【问题讨论】:

    标签: c arrays pointers


    【解决方案1】:

    您将每个单词后的第一个空格替换为空字符。每个单词的第一个字母都有一个指向该单词的指针。 这是标准库函数strtok使用的方法。

    【讨论】:

      猜你喜欢
      • 2021-01-18
      • 2015-06-06
      • 2015-06-11
      • 2015-06-02
      • 1970-01-01
      • 2020-06-11
      • 2011-08-04
      • 1970-01-01
      • 2017-10-15
      相关资源
      最近更新 更多