2012-08-10 116 views
-1

我使用this question爲導向,以修整一個串C.它上正常工作完全由空格(' ')爲界的字符串,但在特殊的空格('\r''\n''\t'等修剪字符串), 它失敗。這裏有一個例子:麻煩在C

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

size_t trim(char *out, size_t len, const char *str) 
{ 
    if(len == 0) 
    return 0; 

    const char *end; 
    size_t out_size; 

    // Trim leading space 
    while(isspace(*str)) str++; 

    if(*str == 0) // All spaces? 
    { 
    *out = 0; 
    return 1; 
    } 

    // Trim trailing space 
    end = str + strlen(str) - 1; 
    while(end > str && isspace(*end)) end--; 
    end++; 

    // Set output size to minimum of trimmed string length and buffer size minus 1 
    out_size = (end - str) < len-1 ? (end - str) : len-1; 

    // Copy trimmed string and add null terminator 
    memcpy(out, str, out_size); 
    out[out_size] = 0; 

    return out_size; 
} 

int main(){ 

    char *str = " \n\n hello \t \r "; 
    char trimmed[strlen(str)]; 

    trim (trimmed, strlen(trimmed), str); 
    printf("~%s~\n~%s~\n", str, trimmed); 

    return 0; 
} 

產生輸出:

~ 

    ~ello   
~~ 

任何人都可以修改代碼以正確修剪所有空格字符?

第二個問題:引用答案中的第一個函數給了我一個段錯誤。有誰知道這是爲什麼?

回答

2

您調用函數的方式不正確。

char *str = " \n\n hello \t \r "; 
char trimmed[strlen(str)+1]; // Note that you must +1 for the terminating \0. 

// Use sizeof() instead of strlen() because trimmed is containing garbage. 
// strlen() measures the length of the content while sizeof() measure the allocated size of the array. 
trim (trimmed, sizeof(trimmed), str); 
0

嘗試isgraph而不是/除了isspace

-1

但在特殊的空白字符( '\ r', '\ n', '\ t' 的等),它失敗。

在C,字符unsigned integers,所以如果你說white-space,會理解' ',其ASCII碼是十進制32或十六進制0x20的。你提到的字符不是空格!

' ' != '\r'

下面是我的實施trimleft-trim,和right-trim功能以除去周圍(即前沿和後沿),前導和chracter串尾部的空格,分別。函數不需要額外的頭文件,這是最依賴整數和指針運算的裸機實現。

您可以修改代碼(在你的情況,你需要擴展邏輯if案件,以涵蓋其他字符,例如\r\n\t等,您在您的文章中提到),根據您的需要。

我將這些功能添加到my string library, zString :)

char *zstring_trim(char *str){ 
    char *src=str; /* save the original pointer */ 
    char *dst=str; /* result */ 
    int in_word=0; /* logical check */ 
    int index=0; /* index of the last non-space char*/ 

    while (*src) 
     if(*src!=' '){ 
     /* Found a word */ 
      in_word = 1; 
      *dst++ = *src++; /* make the assignment first 
           * then increment 
           */ 
     } else if (*src==' ' && in_word==0) { 
     /* Already going through a series of white-spaces */ 
      in_word=0; 
      ++src; 
     } else if (*src==' ' && in_word==1) { 
     /* End of a word, dont mind copy white spaces here */ 
      in_word=0; 
      *dst++ = *src++; 
      index = (dst-str)-1; /* location of the last char */ 
     } 

    /* terminate the string */ 
    *(str+index)='\0'; 

    return str; 
} 

char *zstring_ltrim(char *str){ 
    char *src=str; /* save the original pointer */ 
    char *dst=str; /* result */ 
    int index=0; /* index of the first non-space char */ 

    /* skip leading white-spaces */ 
    for(; *src && *src==' '; ++src, ++index) 
     ; 

    /* copy rest of the string */ 
    while(*src) 
     *dst++ = *src++; 

    /* terminate the string */ 
    *(src-index)='\0'; 

    return str; 
} 

char *zstring_rtrim(char *str){ 
    char *src=str; /* save the original pointer */ 
    char *dst=str; /* result */ 
    int index=0; /* index of the last non-space char*/ 

    /* copy the string */ 
    while(*src){ 
     *dst++ = *src++; 
     if (*src!=' ' && *src) 
      index = (src-str)+1; 
    } 

    /* terminate the string */ 
    *(str+index)='\0'; 

    return str; 
} 
+0

誰**下**投票後有一點ç知識或耐心閱讀代碼的人,他應該已經注意到,所有三個函數僅適用於空格(ASCII碼32位十進制),不適用於\ r,\ n或\ t!這只是修改if條件的問題,或者更好的方法可能是更新函數原型以接受所需的空格字符來移除,如char * zstring_trim(char * str,const char space)。 – 2016-04-03 23:25:29