简体   繁体   English

将Char *转换为Char *数组

[英]Convert Char* to array of Char*

I am trying to convert a Char* to a Char** . 我正在尝试将Char*转换为Char**

eg "echo Hello World" would become {"echo", "Hello", "World"} 例如, "echo Hello World"将变为{"echo", "Hello", "World"}

I know, that I can get the single words from a Char* with strtok() . 我知道,我可以使用strtok()Char*获取单个单词。

But I have problems initializing the Char** , as the Char* is of unknown size, and the single words are of unkown size as well. 但是我在初始化Char**遇到问题,因为Char*的大小未知,并且单个单词的大小也未知。

Your char** is just a pointer to the first char * (or, the beginning of array of char pointers). 您的char**只是指向第一个char *的指针(或char指针数组的开头)。 Allocation of char*[] (it's not the same as char** !!) might be a greater problem. 分配char*[]这是一样char** !!)可能是一个更大的问题。 You should use malloc for this task. 您应该将malloc用于此任务。 If you do not know the number of char* s in advance, you can just guess some size, fill it with NULL s and call realloc when needed. 如果您不预先知道char*的数量,则可以猜测一些大小,用NULL填充它,并在需要时调用realloc

You can run on your string and search ' ' (Space character) then each space you found you can get substring with the function strncpy to get the string between the current space index and the last space index. 您可以在字符串上运行并搜索''(空格字符),然后找到的每个空格都可以使用strncpy函数获取子字符串,以获取当前空格索引和最后一个空格索引之间的字符串。 Each string that you create you can store on "dynamic" array (with malloc and realloc). 您创建的每个字符串都可以存储在“动态”数组中(带有malloc和realloc)。
For the first substring your start index is 0, and at the end of the string you get the last substring between the last space index and the string length. 对于第一个子字符串,起始索引为0,在字符串的末尾,您将获得最后一个空格索引与字符串长度之间的最后一个子字符串。

The first result in a Google search gives you an example that you could modify: Google搜索的第一个结果为您提供了一个您可以修改的示例:

/* strtok example */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>        

int main ()
{
  // allocate 10 strings at a time
  int size = 10;
  int i = 0;
  char str[] ="echo Hello World";
  char** strings = malloc(size * sizeof(char*));
  char* temp;

  printf ("Splitting string \"%s\" into tokens:\n",str);
  temp = strtok (str," ");
  while (temp != NULL)
  {
    strings[i++] = temp;
    temp = strtok (NULL, " ,.-");
    if(i % size == 0)
        //allocate room for 10 more strings
        strings = realloc(strings, (i+size) * sizeof(char*));
  }

  int j;
  for(j = 0; j < i; j ++) 
  {
      printf ("%s\n",strings[j]);
  }
  return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

size_t word_count(const char *str){
    enum { out, in } status;
    size_t count = 0;
    status = out;
    while(*str){
        if(isspace(*str++)){
            status = out;
        } else if(status == out){
            status = in;
            ++count;
        }
    }
    return count;
}

int main(void){
    char original[] = "echo Hello World";
    size_t i, size = word_count(original);
    char *p, **words = (char**)malloc(sizeof(char*)*size);

    for(i = 0, p = original;NULL!=(p=strtok(p, " \t\n")); p = NULL)
        words[i++] = p;
    //check print
    printf("{ ");
    for(i = 0;i<size;++i){
        printf("\"%s\"", words[i]);
        if(i < size - 1)
            printf(", ");
    }
    printf(" }\n");

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM