字符串函数实现(不间断更新)


char *strcpy(char *strdest, const char *str) {

assert(strdest!=NULL);'
assert(str != NULL)
while(*strdest++ = *str++); return strdest; }

 

int main() { char str[10]; strcpy(str, "abcd"); printf("%s\n", str); } 

2.strcmp

#include <stdio.h>

int strcmp(const char *src, const char *dst)
{
         int ret = 0;

		 while(!(ret = *(unsigned char *)src - *(unsigned char *)dst)&&*dst)
			 ++src,++dst;
         
		 if(ret < 0)
			 ret = -1;
		 else if(ret > 0)
			 ret = 1;

		  return ret;
}

int main(void)
{
      printf("%d \n", strcmp("abcddeeef", "abcd"));
	  return 0;
}

3.strcat

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

char  *strcat(char *pDest, const char * pSrc)
{
	char * pDestCpy = pDest + strlen(pDest);
    while((*pDestCpy++ = *pSrc++) != '\0');

	return pDest;
}

int main()
{
    char a[10] = {'a','x','c','v','\0'};

	strcat(a, "efgh");

	printf("%s\n", a);
}

  

posted @ 2014-03-24 17:47  elroyzhang  阅读(169)  评论(0编辑  收藏  举报