2015-03-13 49 views
0

我使用的功能是was posted as an answer on another Stackoverflow question。發佈此的用戶請注意:it does not handle consecutive delimiters如何修改字符串拆分函數以忽略連續分隔符?

我想知道如何修改它,以便它可以處理連續的分隔符?當我有一個額外的delminator時,我基本上忽略它。

例如說我有這樣的事情:

h2,3 d3,4 j3,3 y4,1 g4,3

我想這個分成每個空間的字符串數組,但你可以在某些情況下,看到裏面有多個空格。我只是想忽略額外的分隔符。

編輯:只是要清楚,這是我從我掛到上面的答案使用的代碼:

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

char** str_split(char* a_str, const char a_delim) 
{ 
    char** result = 0; 
    size_t count  = 0; 
    char* tmp  = a_str; 
    char* last_comma = 0; 
    char delim[2]; 
    delim[0] = a_delim; 
    delim[1] = 0; 

    /* Count how many elements will be extracted. */ 
    while (*tmp) 
    { 
     if (a_delim == *tmp) 
     { 
      count++; 
      last_comma = tmp; 
     } 
     tmp++; 
    } 

    /* Add space for trailing token. */ 
    count += last_comma < (a_str + strlen(a_str) - 1); 

    /* Add space for terminating null string so caller 
     knows where the list of returned strings ends. */ 
    count++; 

    result = malloc(sizeof(char*) * count); 

    if (result) 
    { 
     size_t idx = 0; 
     char* token = strtok(a_str, delim); 

     while (token) 
     { 
      assert(idx < count); 
      *(result + idx++) = strdup(token); 
      token = strtok(0, delim); 
     } 
     assert(idx == count - 1); 
     *(result + idx) = 0; 
    } 

    return result; 
} 

int main() 
{ 
    char months[] = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC"; 
    char** tokens; 

    printf("months=[%s]\n\n", months); 

    tokens = str_split(months, ','); 

    if (tokens) 
    { 
     int i; 
     for (i = 0; *(tokens + i); i++) 
     { 
      printf("month=[%s]\n", *(tokens + i)); 
      free(*(tokens + i)); 
     } 
     printf("\n"); 
     free(tokens); 
    } 

    return 0; 
} 
+0

視情況而定,忽略可能不是正確的方法。順序中的兩個分隔符只意味着它們之間有一個空字符串。 – Havenard 2015-03-13 19:49:37

+0

@Havenard這是'strsep()'不同的地方嗎?使用適當的功能。 – 2015-03-13 19:50:52

回答

2

T他應該做的伎倆:如果你想讓它禁用測序分隔符overruning

char** str_split(const char *str, char delimiter) 
{ 
    int len, i, j; 
    char* buf; 
    char** ret; 

    len = strlen(str); 
    buf = malloc(len + 1); 
    memcpy(buf, str, len + 1); 

    j = 1; 
    for (i = 0; i < len; ++i) 
     if (buf[i] == delimiter) 
     { 
      while (buf[i + 1] == delimiter) i++; 
      j++; 
     } 

    ret = malloc(sizeof(char*) * (j + 1)); 
    ret[j] = NULL; 

    ret[0] = buf; 
    j = 1; 
    for (i = 0; i < len; ++i) 
     if (buf[i] == delimiter) 
     { 
      buf[i] = '\0'; 
      while (buf[i + 1] == delimiter) i++; 
      ret[j++] = &buf[i + 1]; 
     } 
    return ret; 
} 

下降兩行while (buf[i + 1] == delimiter) i++;

+0

請問你爲什麼在輸入字符串中使用'const'? – ComputerLocus 2015-03-13 20:55:15

+0

@Fogest這意味着這個函數可以和字符串一起使用。它不會修改您提供的緩衝區。 – Havenard 2015-03-13 21:44:39

0

我覺得strtok()能夠處理您的要求。來自man page

被解析的字符串中的兩個或多個連續分隔符字節的序列被認爲是單個分隔符。

+0

你看過我實際使用的代碼嗎?因爲它已經使用strtok .... – ComputerLocus 2015-03-13 20:03:20

1

將投巨資在回答你的問題,SO說:注意到它不處理連續分隔符,「JAN ,,,二月,三月」 - 但沒有證實的貢獻者的評論。

功能strsep()對待連續的分隔符作爲含有空字段,但功能strtok()忽略(的任意組合)的分隔符集的多個實例。隨着MSVC,我得到這個程序

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

int main(void) 
{ 
    char months[]= "JAN, ,\t , ,FEB,MAR"; 
    char seps[] = ", \t\r\n"; 
    char *sptr; 
    sptr = strtok(months, seps); 
    while (sptr) { 
     printf ("Month is: %s\n", sptr); 
     sptr = strtok(NULL, seps); 
    } 
    return 0; 
} 

輸出:

Month is: JAN 
Month is: FEB 
Month is: MAR 

在您的具體的例子(我懷疑可能包含標籤),這將是

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

int main(void) 
{ 
    char stuff[]= "h2,3  d3,4 j3,3 y4,1 g4,3"; 
    char seps[] = " \t"; 
    char *sptr; 
    sptr = strtok(stuff, seps); 
    while (sptr) { 
     printf ("Stuff is: %s\n", sptr); 
     sptr = strtok(NULL, seps); 
    } 
    return 0; 
} 

輸出:

Stuff is: h2,3 
Stuff is: d3,4 
Stuff is: j3,3 
Stuff is: y4,1 
Stuff is: g4,3 
+0

所以你說我應該使用這個代碼呢?因爲我試圖將每個元素存儲到一個數組中。使用我提供的示例數據行我應該得到一個數組,如下所示:'[「h2,3」,「d3,4」,「j3,3」,「y4,1」,「g4,3」]' – ComputerLocus 2015-03-13 20:15:51

+0

添加了您的具體示例。 – 2015-03-13 20:16:54

+0

在那裏沒有看到它? – ComputerLocus 2015-03-13 20:22:23

相關問題