/* 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;
}