2010-06-11 47 views
1
char testStr[] = "   trim this   "; 
char** pTestStr = &testStr; 
trim(pTestStr); 

int trim(char** pStr) 
{ 
char* str = *pStr; 
while(isspace(*str)) { 
    (*pStr)++; 
    str++; 
} 

if(*str == 0) { 
    return 0; 
} 

char *end = str + strlen(str) - 1; 
while(end > str && isspace(*end)) 
    end--; 
*(end+1) = 0; 

return 0; 
} 
+0

重複:http://stackoverflow.com/questions/122616/painless-way-to-trim-leading-trailing-whitespace-in-c – indiv 2010-06-17 19:37:48

回答

2
char testStr[] = "   trim this   "; 
char* pTestStr = &testStr[0]; 
trim(&pTestStr); 

void trim(char* str) 
{ 
    if(!str) 
     return; 

    char* ptr = str; 
    int len = strlen(ptr); 

    while(len-1 > 0 && isspace(ptr[len-1])) 
     ptr[--len] = 0; 

    while(*ptr && isspace(*ptr)) 
     ++ptr, --len; 

    memmove(str, ptr, len + 1); 
} 
4

你需要讓testStr可寫:

char testStr[] = "   trim this   "; 

的問題是,char *ptr = ...ptr指向實際文字字符串是在只讀存儲器中。

通過使用char testStr[] = ...您正在分配一個數組,並使用與文字字符串相同的內容初始化數組。由於這是一個數組,它是可寫的。

+0

我得到: 無法從「字符轉換參數1( *)[38]'到'char **' – theanine 2010-06-14 17:37:04

+0

@ user364100 - 不,你不能。你需要將你的函數改成'char * trim(char * pStr)',然後將指針返回給第一個非空白字符。 – 2010-06-14 20:23:41

+0

我不想那樣。我想要一個適合修剪的修剪功能。 – theanine 2010-06-15 20:26:59

0

編輯:更新基於zString庫的最新版本的代碼。

以下是我對trim,left-trimright-trim函數(將被添加到zString string library)的實現。

儘管函數返回*char,由於原始字符串被修改,所以這些將用於您的目的。

可以使用標準庫函數,如isspace(),但下面的實現是裸露骨代碼,它依賴於庫函數。

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

    /* validate input */ 
    if (!str) 
     return str; 

    while ((c=*src)){ 
     is_space=0; 

     if (c=='\t' || c=='\v' || c=='\f' || c=='\n' || c=='\r' || c==' ') 
      is_space=1; 

     if(is_space == 0){ 
     /* Found a word */ 
      in_word = 1; 
      *dst++ = *src++; /* make the assignment first 
           * then increment 
           */ 
     } else if (is_space==1 && in_word==0) { 
     /* Already going through a series of white-spaces */ 
      in_word=0; 
      ++src; 
     } else if (is_space==1 && 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; 
} 

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

    /* validate input */ 
    if (!str) 
     return str; 

    /* copy the string */ 
    while(*src){ 
     *dst++ = *src++; 
     c = *src; 

     if (c=='\t' || c=='\v' || c=='\f' || c=='\n' || c=='\r' || c==' ') 
      is_space=1; 
     else 
      is_space=0; 

     if (is_space==0 && *src) 
      index = (src-str)+1; 
    } 

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

    return str; 
} 

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

    /* validate input */ 
    if (!str) 
     return str; 

    /* skip leading white-spaces */ 
    while((c=*src)){ 
     if (c=='\t' || c=='\v' || c=='\f' || c=='\n' || c=='\r' || c==' '){ 
      ++src; 
      ++index; 
     } else 
      break; 
    } 

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

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

    return str; 
}