C言語文字列操作関数

1924 ワード

/*	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)
",src_str_len); printf("dst_str_len(%d)
",dst_str_len); #ifdef no_len_limit { //strcpy NULL copy , hello // copy, dst_string printf("dst_string(%s)
",dst_string); strcpy(dst_string,src_string); printf("dst_string(%s)
",dst_string); //strcat NULL cat strcat(dst_string,src_string); printf("dst_string(%s)
",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)
",dst_string); strncpy(dst_string,src_string,strlen(src_string)); //strncpy(dst_string,src_string,strlen(src_string)+1); printf("dst_string(%s)
",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)
",pos); pos = strrchr(dst_string,'r'); if(pos != NULL) printf("pos_strrchr(%s)
",pos); pos = strpbrk(dst_string,"abcde"); if(pos != NULL) printf("pos_strpbrk(%s)
",pos); } #endif #ifdef find_string { char *pos = NULL; pos = strstr(dst_string,"rd"); if(pos != NULL) printf("pos_strstr(%s)
",pos); } #endif return 0; }