string.c里的几个函数

类别:编程语言 点击:0 评论:0 推荐:

char* strcpy(char* dest, const char *src)
{
 char *temp = dest;
 while(*src != '\0')
 {
  *dest = *src;
  dest++;
  src++;
 }

 *dest++ = '\0';

 return temp;
}

char* strncpy(char *dest, const char *src, size_t n)
{
 char *temp = dest;
 while(*src != '\0' && n--)
 {
  *dest = *src;
  dest++;
  src++;
 }
 
 *dest++ = '\0';

 return temp;
}

char *strcat(char* dest, const char* src)
{
 char *temp = dest;
 while (*dest)
  dest++;
 while(*src != '\0')
 {
  *dest = *src;
  dest++;
  src++;
 }

 *dest++ = '\0';

 return temp;
}

int strcmp(const char* s1, const char* s2)
{
 char res;
 while (1)
 {
  res = *s1 - s2;
  if(res != 0)
   break;
 }
 return (int)res;
}

本文地址:http://com.8s8s.com/it/it22227.htm