C语言字符串工具箱DIY之剔除字符串首尾的空白字…
2018-06-18 03:50:00来源:未知 阅读 ()
@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] = '\0';
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] = {
" \t 2\t2 \n \v \t ", // 12 23
" \t 22 \n \v \t ", // 1223
" \t 2 \n \v \t ", // 123
" \t \n \v \t " // 13
};
int i = 0;
while(i < 4){
char dest_buf[8] = {'\0'};
printf("%s\n",
STR_CAT3(dest_buf,
str1,
str_trim(*(str2+i)),
str3)
);
++i;
}
printf("%d\n", isspace('\0')); // 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 '\0') 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 '\0'. 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) |
'\t' | (0x09) | horizontal tab (TAB) |
'\n' | (0x0a) | newline (LF) |
'\v' | (0x0b) | vertical tab (VT) |
'\f' | (0x0c) | feed (FF) |
'\r' | (0x0d) | carriage return (CR) |
(5) Details of Indexed Comments (Like @Comment_N) Above
- 原字符串全部可访问的下标范围为闭区间[0, strlen(str)];
- printf("%d\n", isspace('\0')); 会输出0, 即'\0'不被作为空白字符;
- 满是空白字符,精简后即是空字符串咯;
- 因为isspace('\0')为假, 因此应直接从'\0'的前一个字符开始检测空白;
- 精简后得到的新字符串全部可访问的下标范围为闭区间[0, new_lenth];
Copyright NOT Fully Reserved ? yawenunion@whatever.com On 20180403
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 关于各种不同开发语言之间数据加密方法(DES,RSA等)的互通的 2020-06-07
- C语言程序结构 2020-05-31
- 每日干货丨C++语言主流开发工具推荐! 2020-04-28
- C语言实现经典游戏——扫雷! 2020-04-17
- Z 字形变换 2020-04-14
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash