strcpy函数在android中的实现

一. 头文件声明:
string.h:

#ifndef _STRING_H_
#define _STRING_H_

#include <sys/cdefs.h>
#include <stddef.h>
#include <malloc.h>

extern char*  strcpy(char *, const char *);
extern char*  strcat(char *, const char *);
......

#endif /* _STRING_H_ */

 

二.函数实现:

1. strcat
#include <string.h>
char *strcat(char *s, const char *append)
{
 char *save = s;

 for (; *s; ++s);
 while ((*s++ = *append++) != '/0');
 return(save);
}

 

2. strcpy
#include <string.h>

char *strcpy(char *to, const char *from)
{
 char *save = to;

 for (; (*to = *from) != '/0'; ++from, ++to);
 return(save);
}

 

3. strlen
size_t strlen(const char *str)
{
 const char *s;

 for (s = str; *s; ++s)
  ;
 return (s - str);
}

 

4. strcmp
int strcmp(const char *s1, const char *s2)
{
 while (*s1 == *s2++)
  if (*s1++ == 0)
   return (0);
 return (*(unsigned char *)s1 - *(unsigned char *)--s2);
}

 

5. strstr
char *strstr(const char *s, const char *find)
{
 char c, sc;
 size_t len;

 if ((c = *find++) != 0) {
  len = strlen(find);
  do {
   do {
    if ((sc = *s++) == 0)
     return (NULL);
   } while (sc != c);
  } while (strncmp(s, find, len) != 0);
  s--;
    }
 return ((char *)s);
}

 

6. strncpy
char *strncpy(char *dst, const char *src, size_t n)
{
 if (n != 0) {
  char *d = dst;
  const char *s = src;

  do {
   if ((*d++ = *s++) == 0) {
    /* NUL pad the remaining n-1 bytes */
    while (--n != 0)
     *d++ = 0;
      break;
  }
  } while (--n != 0);
 }
 return (dst);
}

 

7. strncmp
int strncmp(const char *s1, const char *s2, size_t n)
{

 if (n == 0)
  return (0);
 do {
  if (*s1 != *s2++)
   return (*(unsigned char *)s1 - *(unsigned char *)--s2);
  if (*s1++ == 0)
      break;
 } while (--n != 0);
 return (0);
}

 

8. strncat
char *strncat(char *dst, const char *src, size_t n)
{
 if (n != 0) {
  char *d = dst;
  const char *s = src;

  while (*d != 0)
   d++;
  do {
   if ((*d = *s++) == 0)
    break;
   d++;
  } while (--n != 0);
  *d = 0;
  }
 return (dst);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值