/* file name : sting_func_test.c
author : zhongjun
description :sting_func_test demo
data :20150701
time :PM 22:36
key(study) :string operate
note :所有模块单独测试,没有试过一起测试,可能会memory fault
*/
#include <string.h>
#include <stdio.h>
char dst_string[20] = "hard work";
char *src_string = "hello";
int main()
{
size_t src_str_len = 0;
size_t dst_str_len = 0;
//strlen 返回值不包含NULL,此值返回5
src_str_len = strlen(src_string);
dst_str_len = strlen(dst_string);
printf("src_str_len(%d)\n",src_str_len);
printf("dst_str_len(%d)\n",dst_str_len);
#ifdef no_len_limit
{
//strcpy 会把NULL也copy过去,输出hello
//如果copy,要保证dst_string有足够的空间
printf("dst_string(%s)\n",dst_string);
strcpy(dst_string,src_string);
printf("dst_string(%s)\n",dst_string);
//strcat 会从上个字符串的NULL开始cat
strcat(dst_string,src_string);
printf("dst_string(%s)\n",dst_string);
}
#endif
#ifdef len_limit
{
//strncpy copy strlen(src_string)不会copy NULL
//strncpy copy len > strlen(src_string) copy NULL,多余的会填充NULL
printf("dst_string(%s)\n",dst_string);
strncpy(dst_string,src_string,strlen(src_string));
//strncpy(dst_string,src_string,strlen(src_string)+1);
printf("dst_string(%s)\n",dst_string);
//strncat strncmp同样是带 len limit的
}
#endif
#ifdef find_char
{
//strchr 返回找到第一个位置,
//strrchr 返回找到最后一个位置
//strpbrk 找一个字符 group
char *pos = NULL;
pos = strchr(dst_string,'r');
if(pos != NULL)
printf("pos_strchr(%s)\n",pos);
pos = strrchr(dst_string,'r');
if(pos != NULL)
printf("pos_strrchr(%s)\n",pos);
pos = strpbrk(dst_string,"abcde");
if(pos != NULL)
printf("pos_strpbrk(%s)\n",pos);
}
#endif
#ifdef find_string
{
char *pos = NULL;
pos = strstr(dst_string,"rd");
if(pos != NULL)
printf("pos_strstr(%s)\n",pos);
}
#endif
return 0;
}