C語言字串工具箱DIY之剔除字串首尾的空白字元的str_trim函式

yawenunion發表於2018-04-03

@Header File Named “string_toolbox.h

Contents of File “string_toolbox.h”  

Are as follows:

#ifndef STRING_TOOLBOX_H_INCLUDED

#define STRING_TOOLBOX_H_INCLUDED

char *str_trim(const char *str);

#endif

 

@Source File Named “string_toolbox.c

Contents of File “string_toolbox.c”  

Are as follows:

#include “string_toolbox.h”

#include <string.h>
#include <ctype.h>
#include <stdlib.h>

char *str_trim(const char *str)
{
    size_t

i = 0,

j = strlen(str),

k = 0,

new_lenth;

// @Comment_1 

    while(isspace(str[i])){

      // @Comment_2

         ++i;
    }

   if(i == j) return “”;

      // @Comment_3

    while(isspace(str[–j]));

      // @Comment_4

    new_lenth = j – i + 1;

      // @Comment_5

    char *result = (char *)

      malloc(new_lenth + 1);

    while(k < new_lenth){
         result[k++] = str[i++];
    }
    result[k] = ` `;

    return result;
}

 

@Source File  for Testing Named “main.c

Contents of File “main.c”  

Are as follows:

 

#include “string_toolbox.h”
#include <ctype.h>
#include <string.h>

#define STR_CAT3(d, s1, s2, s3) (strcat(strcat(strcat(d, s1), s2), s3))

int main()
{
    char
        *str1 = “1”,
        *str3 = “3”,
        *str2[4] = {
         ” 2 2
v “, // 12  23

         ” 22
v “, // 1223

         ” 2
v “, // 123

        
v ” // 13

        };

    int i = 0;
    while(i < 4){
        char dest_buf[8] = {` `};
        printf(“%s
“,

      STR_CAT3(dest_buf,

      str1,

      str_trim(*(str2+i)),

      str3)

     );
        ++i;
    } 

    printf(“%d
“, isspace(` `)); // 0

 

    return 0;
}

 

Postscripts

(1) size_t is type_alias of “unsigned int“.

(2) Function void *malloc(size_t n_bytes); is declared in <stdlib.h>.

  It is used to dynamically allocate memory.

(3) Function size_t strlen(const char *str); is declared in <string.h>.

  It counts how many valid charaters (that is the amount of charaters before ` `) are in this string.

(4) Function char *strcat(char *destination_string_buf, const char *source_string); is  declared in <string.h>.

  It appends the source_string to the ending of the destination_string_buf on ` `. The destination_string_buf should contain a C string, and be large enough to contain the concatenated resulting string.

(5) Function int isspace(int c); is declared in <ctype.h>.

  It checks whether c is a white-space character. For the “C” locale, white-space characters are any of :

` ` (0x20) space (SPC)
` ` (0x09) horizontal tab (TAB)
`
`
(0x0a) newline (LF)
`v` (0x0b) vertical tab (VT)
`f` (0x0c) feed (FF)
`
`
(0x0d) carriage return (CR)

(5) Details of Indexed Comments (Like @Comment_N) Above

  1. 原字串全部可訪問的下標範圍為閉區間[0, strlen(str)];
  2. printf(“%d
    “, isspace(` `));
     會輸出0, 即` `不被作為空白字元;
  3. 滿是空白字元,精簡後即是空字串咯;
  4. 因為isspace(` `)為假, 因此應直接從` `的前一個字元開始檢測空白;
  5. 精簡後得到的新字串全部可訪問的下標範圍為閉區間[0, new_lenth];

 

Copyright NOT Fully Reserved © yawenunion@whatever.com On 20180403

相關文章