目录
0.前言
1.函数介绍及其模拟实现
1.1strlen
1.2 strcpy
1.3 strcat
1.4 strcmp
1.5 strstr
1.6 memcpy
0.前言
C语言对字符和字符串的处理很频繁,但是C语言本身是没有字符串类型的,字符串通常存放在字符数组或者常量字符串中.
1.函数介绍及其模拟实现 1.1strlen先来strlen在msdn上的介绍
size_t strlen ( const char * str );
~ 字符串已经'\0'作为结束标志,strlen函数返回的是字符串中'\0'前面出现的字符个数(不包含'\0')
~ 参数指向的字符串必须要以'\0'结束
~注意函数的返回值为size_t,是无符号的
#include
int main()
{
const char*str1 = "abcdef";
const char*str2 = "bbb";
if(strlen(str2)-strlen(str1)>0)
{
printf("str2>str1\n");
}
else
{
printf("srt1>str2\n");
}
return 0;
}
strlen的模拟实现 -- 3种方法
//模拟实现strlen
//1.循环的方法
int my_strlen(const char* arr)
{
assert(arr);
int ret = 0;
while (*arr)
{
arr++;
ret++;
}
return ret;
}
//2.递归的方法
int my_strlen(const char* arr)
{
assert(arr);
if (*arr)
return my_strlen(arr + 1) + 1;
else
return 0;
}
//3.指针-指针的方法
int my_strlen(const char* arr)
{
assert(arr);
char* tmp = arr;
while (*arr)
{
arr++;
}
return arr - tmp;
}
1.2 strcpy
strcpy在MSDN上的介绍
char* strcpy(char * destination, const char * source );
~ The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination.
~ 源字符串必须以'\0'结束
~ 目标空间必须足够打,以确保能存放源字符串
~ 目标空间必须可变
strcpy的模拟实现
char* my_strcpy(char* dest, const char* src)
{
assert(dest && src);
char* tmp = *dest;
while (*dest++ = *src++)
{
;
}
return dest;
}
1.3 strcat
strcat在MSDN上的介绍
char * strcat ( char * destination, const char * source );
~ The strcat function appends strSource to strDestination and terminates the resulting string with a null character. The initial character of strSource overwrites the terminating null character of strDestination.
~ 源字符串必须以'\0'结束
~ 目标空间必须足够大,能容纳下源字符串的内容
~ 目标空间必须可修改
/* strncat example */
#include
#include
int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strncat (str1, str2, 6);
puts (str1);
return 0;
}
模拟实现strcat
char* my_strcat(char* dest,const char* src)
{
assert(dest && src);
char* str = dest;
while (*dest)
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return str;
}
1.4 strcmp
strcmp在MSDN上的介绍
int strcmp ( const char * str1, const char * str2 );
~ The strcmp function compares string1 and string2 lexicographically and returns a value indicating their relationship
~
Value Relationship of string1 to string2 < 0 string1 less than string2 0 string1 identical to string2 > 0 string1 greater than string2
模拟实现strcmp
int my_strcmp(const char * arr1,const char* arr2)
{
assert(arr1 && arr2);
while (*arr1 == *arr2)
{
if (*arr1 == '\0')
return 0;
arr1++;
arr2++;
}
if (*arr1 > *arr2)
return 1;
else
return -1;
}
1.5 strstr
char * strstr ( const char *str1, const char * str2);
~ The strstr function returns a pointer to the first occurrence of strCharSet in string. The search does not include terminating null characters
/* strstr example */
#include
#include
int main ()
{
char str[] ="This is a simple string";
char * pch;
pch = strstr (str,"simple");
strncpy (pch,"sample",6);
puts (str);
return 0;
}
模拟实现strstr
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* s1 = str1;
const char* s2 = str2;
const char* cur = str1;
while (*cur)
{
s1 = cur;
s2 = str2;
while ((*s1 == *s2) && *s1 && *s2)
{
s1++;
s2++;
}
if (*s2 == 'void* my_memcpy(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
')
{
return (char*)cur;
}
cur++;
}
return NULL;
}
1.6 memcpy
void * memcpy ( void * destination, const void * source, size_t num );
~ 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置
~ 这个函数在遇到'\0'的时候并不会停下来
~ 如果source和destination有任何重叠,复制的结果都是未定义的
/* memcpy example */
#include
#include
struct {
char name[40];
int age;
} person, person_copy;
int main ()
{
char myname[] = "Pierre de Fermat";
/* using memcpy to copy string: */
memcpy ( person.name, myname, strlen(myname)+1 );
person.age = 46;
/* using memcpy to copy structure: */
memcpy ( &person_copy, &person, sizeof(person) );
printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );
return 0;
}
模拟实现memcpy
void* my_memove(void* dest,const void* src,size_t num)
{
assert(dest && src);
void* tmp = dest;
if (dest < src)
{
while (num --)
{
*(char *)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
while (num --)
{
*((char*)dest+num) = *((char*)src + num);
}
}
return tmp;
}
1.7 memmove
void * memmove ( void * destination, const void * source, size_t num );
~ 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的
~ 如果源空间和目标空间出现重叠,就得使用memmove函数处理
/* memcpy example */
#include
#include
struct {
char name[40];
int age;
} person, person_copy;
int main ()
{
char myname[] = "Pierre de Fermat";
/* using memcpy to copy string: */
memcpy ( person.name, myname, strlen(myname)+1 );
person.age = 46;
/* using memcpy to copy structure: */
memcpy ( &person_copy, &person, sizeof(person) );
printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );
return 0;
}
memmove的模拟实现
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)