函数模拟实现

目录

一、strlen

1、strlen函数的使用

​编辑

2、strlen函数的模拟实现

二、strcpy

1、strcpy函数的使用

2、strcpy函数的模拟实现

三、strcat

1、strcat函数的使用

​编辑

2、strcat函数的模拟实现

四、strcmp

1、strcmp函数的使用

​编辑

2、strcmp函数的模拟实现

五、strstr

1、strstr函数的使用

​编辑

2、strstr函数的模拟实现

六、strncpy

1、strncpy函数的使用

​编辑

2、strncpy函数的模拟实现

七、strncat

1、strncat函数的使用

​编辑

2、strncat函数的模拟实现

八、memcpy

1、memcpy函数的使用

​编辑

2、memcpy函数的模拟实现

九、memmove

1、memmove函数的使用

​编辑

2、memmove函数的模拟实现


一、strlen
1、strlen函数的使用
size_t strlen ( const char * str );

返回C字符串str的长度。

C字符串的长度由结束的空字符决定:C字符串的长度等于字符串开头和结束的空字符之间的字符数(不包括结束的空字符本身)。

不应将此与保存字符串的数组的大小混淆。例如:

Char mystr[100]="测试字符串";

定义了一个长度为100个字符的字符数组,但是mystr初始化的C字符串的长度只有11个字符。因此,sizeof(mystr)的结果是100,而strlen(mystr)的结果是11。

/* strlen 例子 */
#include <stdio.h>
#include <string.h>

int main ()
{
  char szInput[256];
  printf ("Enter a sentence: ");
  gets (szInput);
  printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
  return 0;
}
2、strlen函数的模拟实现
int my_strlen(char* s)
{
	char* p = s;
	while (*p != '\0')//读到‘\0’为止
		p++;
	return p - s;//指针相减得到str的长度
}
二、strcpy
1、strcpy函数的使用
char * strcpy ( char * destination, const char * source );

将source指向的C字符串复制到destination指向的数组中,包括结束的null字符(并在该点停止)。为了避免溢出,destination指向的数组的大小应该足够长,以包含与source相同的C字符串(包括结束的null字符),并且不应该在内存中与source重叠。

/* strcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

2、strcpy函数的模拟实现
char* my_strcpy(char* str2, const char* str1)
{
	while ( *str1-1 != '\0')
	{
		*str2 = *str1;
		str2 ++;
        str1 ++;
	}
	return 0;
}
三、strcat
1、strcat函数的使用
char * strcat ( char * destination, const char * source );

将源字符串的副本追加到目标字符串。destination中的结束null字符被source的第一个字符覆盖,并且在destination中由两者串联形成的新字符串的末尾包含一个空字符。
目的地和源头不得重叠。

/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}
2、strcat函数的模拟实现
char* my_strcat(char* dest, const char* src)//char*表明函數返回目標數組的首地址
{
	char* ret = dest;
	while (*dest != '\0') {       //1、找到目標字符串的末尾即‘\0’位置
		dest++;
	}
	while (*src) 
	{
		*dest++ = *src++;
	}  //2、將源字符串複製到目標字符串後空間
	return ret;               //3、返回目標字符串首地址
}
四、strcmp
1、strcmp函数的使用
int strcmp ( const char * str1, const char * str2 );

比较C字符串str1和C字符串str2。
这个函数开始比较每个字符串的第一个字符。如果它们彼此相等,则继续执行以下对,直到字符不同或达到终止空字符为止。
这个函数执行字符的二进制比较。有关考虑特定于语言环境规则的函数,请参见stroll。

#include <stdio.h>
#include <string.h>

int main ()
{
  char key[] = "apple";
  char buffer[80];
  do {
     printf ("Guess my favorite fruit? ");
     fflush (stdout);
     scanf ("%79s",buffer);
  } while (strcmp (key,buffer) != 0);
  puts ("Correct answer!");
  return 0;
}
2、strcmp函数的模拟实现
int my_strcmp(const char* str1, const char* str2)
{
	assert(str1, str2);		
	{
		if (*str1 == '\0')	
		{
			return 0;
		}
		str1++; str2++;
	}
	return *str1 - *str2;
	
}
五、strstr
1、strstr函数的使用
const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );

返回指向str1中str2第一次出现的指针,如果str2不是str1的一部分,则返回空指针。
匹配过程不包括结束的空字符,但它到此为止。

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  if (pch != NULL)
    strncpy (pch,"sample",6);
  puts (str);
  return 0;
}
2、strstr函数的模拟实现
char* my_strstr(char* str1, const char* str2)
{
	assert(str1 && str2);
	if (str2 == '\0')
	{
		return str1;
	}
	char* p;//用于记录开始遍历的起始位置
	char* s1;//遍历str1的指针
	char* s2;//遍历str2的指针
	p = str1;//记录起始位置
	while(*p)
	{
		s1 = p;
		s2 = str2;
		while (*s1==*s2&&*s1&&*s2)
		{
			s1++;
			s2++;

		}
		if (*s2 == '\0')
		{
			return p;
		}
		p++;

	}
	return NULL;
}
六、strncpy
1、strncpy函数的使用
char * strncpy ( char * destination, const char * source, size_t num );

将源的第一个num字符复制到目标。如果在复制num个字符之前找到源C字符串的结尾(用空字符表示),则目的地将用零填充,直到向其写入总数为num个字符。
如果source大于num,则不会在destination的末尾隐式添加空字符。因此,在这种情况下,destination不应被视为以空结束的C字符串(这样读取会溢出)。
目的和源不应重叠(当重叠时,请参阅memmove以获得更安全的替代方案)。

/* strncpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]= "To be or not to be";
  char str2[40];
  char str3[40];

  /* copy to sized buffer (overflow safe): */
  strncpy ( str2, str1, sizeof(str2) );

  /* partial copy (only 5 chars): */
  strncpy ( str3, str2, 5 );
  str3[5] = '\0';   /* null character manually added */

  puts (str1);
  puts (str2);
  puts (str3);

  return 0;
}
2、strncpy函数的模拟实现
char*  my_strncpy(char* destination, const char* source, size_t num)
{
	assert(source && destination);
	while (num--)
	{
		*destination = *source;
		destination++;
		source++;

	}
	return 0;
}
七、strncat
1、strncat函数的使用
char * strncat ( char * destination, const char * source, size_t num );

将源的第一个num字符附加到目标,加上一个结束的空字符。
如果source中的C字符串的长度小于num,则只复制结束空字符之前的内容。

/* strncat example */
#include <stdio.h>
#include <string.h>

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;
}
2、strncat函数的模拟实现
char* my_strncat(char* destination, const char* source, size_t num)
{
	assert(source && destination);
	while (*(destination-1))
	{
		destination++;
	}
	while (num--)
	{
		*destination = *source;
		destination++;
		source++;
	}

	return destination;
}
八、memcpy
1、memcpy函数的使用
void * memcpy ( void * destination, const void * source, size_t num );

将num字节的值从源指向的位置直接复制到目标指向的内存块。
源指针和目标指针所指向的对象的底层类型与此函数无关;结果是数据的二进制副本。
该函数不检查源中是否有任何终止null字符——它总是精确地复制num个字节。
为了避免溢出,目的参数和源参数所指向的数组的大小至少为num字节,并且不应该重叠(对于重叠的内存块,为memm).

/* memcpy example */
#include <stdio.h>
#include <string.h>

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;
}
2、memcpy函数的模拟实现
void*  my_memcpy(void* destination, const void* source, size_t num)
{
	assert(destination && source);
	void* ret = destination;
	while (num--)
	{
		*(char*)destination = *(char*)source;
		(char*)destination= (char*)destination+1;
		(char*)source= (char*)source+1;

	}
	return ret;
}
九、memmove
1、memmove函数的使用
void * memmove ( void * destination, const void * source, size_t num );

将num字节的值从源指向的位置复制到目标指向的内存块。复制就像使用了中间缓冲区一样进行,从而允许目标和源重叠。
源指针和目标指针所指向的对象的底层类型与此函数无关;结果是数据的二进制副本。
该函数不检查源中是否有任何终止null字符——它总是精确地复制num个字节。
为避免溢出,目标参数和源参数所指向的数组的大小为s。

/* memmove example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "memmove can be very useful......";
  memmove (str+20,str+15,11);
  puts (str);
  return 0;
}
2、memmove函数的模拟实现
void* my_memmove(void* destination, const void* source, size_t num)
{
	assert(destination && source);
	void* str = NULL;
	size_t i = num;
	void* ret = destination;


	if (destination <= source)//从前往后拷贝
	{
		while (num--)
		{
			*(char*)destination = *(char*)source;
			(char*)destination = (char*)destination + 1;
			(char*)source = (char*)source + 1;
		}
	}
	else//从后往前拷贝
	{
		while (num--)
		{
			*((char*)destination + num) = *((char*)source + num);
		}
	}
	return ret;
}
  • 26
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值