【问题标题】:How to transform a block of strings to an array of strings如何将字符串块转换为字符串数组
【发布时间】:2013-04-06 15:48:01
【问题描述】:

我有一个字符串块,比如“aaa\0bbbb\0ccccccc\0” 我想把它们变成一个字符串数组。 我尝试使用以下代码这样做:

void parsePath(char* pathString){
  char *pathS = malloc(strlen(pathString));
  strcpy(pathS, pathString);
  printf(1,"33333\n");
  pathCount = 0;
  int i,charIndex;
  printf(1,"44444\n");
  for(i=0; i<strlen(pathString) ; i++){
      if(pathS[i]=='\0')
      {
       char* ith = malloc(charIndex);
       strcpy(ith,pathS+i-charIndex);
       printf(1,"parsed string %s\n",ith);
       exportPathList[pathCount] = ith;
       pathCount++;
       charIndex=0;
      }
      else{
        charIndex++;
      }
  }

  return;
}

exportPathList 是前面代码中定义的全局变量 char* exportPathList[32]; 使用该函数时 exportPathList[i] 包含垃圾。 我做错了什么?

【问题讨论】:

  • 当你执行 strlen(pathString) 时,你只会得到第一个字符串的长度,而这就是 strcpy 将复制的所有内容。而且您永远不会记录“最后一个”字符串,因为您在 strlen 处停止,因此永远不会看到终止的 null。
  • 直到你不知道该块包含多少个字符串,你才能做到。
  • 您基本上需要通过其他方式知道输入字符串的长度。
  • 您需要使用双空值终止您的输入字符串。否则,您将无法知道字符串数组的结束位置。

标签: c arrays string


【解决方案1】:

这个 SO 问题的答案:

Parse string into argv/argc

处理类似的问题,你可以看看。

您需要知道有多少字符串或同意“字符串结尾”。最简单的方法是在末尾添加一个空字符串:

 aaa\0bbbb\0ccccccc\0\0
                     ^^

附:这是作业吗?

【讨论】:

  • 您不必手动在字符串末尾添加两个\0,只需一个。第二个自动存在,因为它是字符串文字。
【解决方案2】:

首先,由于您的字符串由空字符分隔,'\0'strlen 将只报告字符串的大小,直到第一个 '\0'strcpy 也会复制到第一个空字符。

此外,您无法知道输入字符串以该信息结尾的位置。您要么需要传递整个大小,要么例如以双空字符结束输入:

#include <stdio.h>
#include <string.h>
void parsePath(const char* pathString){
    char buf[256]; // some limit
    while (1) {
        strcpy(buf, pathString);
        pathString+=strlen(buf) + 1;
        if (strlen(buf) == 0)
            break;
        printf("%s\n", buf);
    }   
}

int main()
{
    const char *str = "aaa\0bbbb\0ccccccc\0\0";
    parsePath(str);
    return 0;
}

而且你需要一些 realloc 来实际创建数组。

【讨论】:

    【解决方案3】:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXSIZE 16
    
    char* exportPathList[MAXSIZE] = {0};
    size_t pathCount = 0;
    
    void parsePath(char* pathString){
        char *ptop, *pend;
    
        ptop=pend=pathString;
        while(*ptop){
            while(*pend)++pend;
            exportPathList[pathCount++]=strdup(ptop);
            pend=ptop=pend+1;
        }
    }
    
    int main(){
        char textBlock[]= "aaa\0bbbb\0ccccccc\0";
        //size_t size = sizeof(textBlock)/sizeof(char);
        int i;
    
        parsePath(textBlock);
        for(i=0;i<pathCount;++i)
            printf("%s\n", exportPathList[i]);
    
        return 0;
     }
    

    【讨论】:

      【解决方案4】:

      我实现的解决方案确实是在字符串末尾添加双 '\0' 并使用它来计算字符串的数量。

      我的新实现(路径是字符串的数量):

      void parsePath(char* pathString,int paths){
        int i=0;  
        while (i<paths) {
          exportPathList[i] = malloc(strlen(pathString)+1);
          strcpy(exportPathList[i], pathString);
          pathString+=strlen(pathString);
          i++;
          }  
      }
      

      我要感谢所有做出贡献的人。

      【讨论】:

        【解决方案5】:

        我的实现看起来像这样 -> 它在 main 函数中遵循 argv 和 argc 的想法:

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        int main(void){
            char **args = (char**)malloc(100*sizeof(char));
            char buff[100], input_string[100], letter;
            
            for(int i = 0; i < 100; i++){
                
                buff[i] = '\0';
                input_string[i] = '\0';
            }
            for(int i = 0; (letter = getchar())!='\n'; i++){
                input_string[i] = letter;
            }
            
            int args_num = 0;
            for(int i = 0, j = 0; i < 100;i++){
                
                if((input_string[i] == ' ')||(input_string[i]=='\0')){
                    //reset j = 0
                    j = 0;
                    args[args_num] = malloc(strlen(buff+1));
                    strcpy(args[args_num++],buff);
                    for(int i = 0; i < 100; i++)buff[i] = '\0';
                }else buff[j++] = input_string[i];
                
            }    
            
            for(int i = 0; i < args_num; i++){
                printf("%s ",args[i]);
            }    
        }
        

        -> 然后可以使用 args[i] 访问字符串中的每个单词

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-13
          • 2011-06-18
          • 2017-12-01
          • 1970-01-01
          • 2017-11-27
          相关资源
          最近更新 更多