文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档> strcpy、strcmp、strcat等字符串函数实现

strcpy、strcmp、strcat等字符串函数实现

时间:2010-09-07  来源:huazaicola

一些字符串函数的实现

这些函数实现最好参考linux内核源码,向大师学习!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *str_cpy(char *dest, const char *src)
{
    int i = 0;
    while(*dest++ = *src++);
    return dest;
}
char str_rev(char *s)
{
    
    char *d = s + strlen(s) - 1;
    char temp;
    while(d > s)
    {
        temp = *d;
        *d = *s;
        *s = temp;
        d--;
        s++;
    }
    
}
static int str_len(const char *s)
{
    int len = 0;
    while(*s++)
        len++;
    return len;
}

static char *str_cat(char *dest, const char *src)
{
    char *const old = dest;
    while(*dest)
        dest++;
    while(*dest++ = *src++);
    return old;
}
/*static int str_cmp(const char *dst, const char *src)
{    int i = 0;
    while(*(dst + i) == *(src + i))    
    {
        if(*(src+i) == '\0')
            return 0;
        i++;
    }
    if(*dst > *src)
        return 1;
    else
        return -1;
}*/
int str_cmp(const char * cs, const char * ct)
{
    signed char __res;
    while(1)
    {
        if((__res = *cs - *ct++) != 0 || !cs++)
            break;
    }
    return __res;
}

int main(void)
{
    char a[20]="hello";
    char b[10];
    char c[]="hellz";

    str_cpy(b,a);
    printf("str_cpy:b<-a:%s \n", b);

    str_rev(a);
    printf("str_rev:a=%s \n", a);

    printf("str_cmp:%d \n",str_cmp(a, c));

    str_len(b);    
    printf("str_len:b=%d \n", str_len(b));
    
    printf("strcat:a+b:%s \n", str_cat(a,c));

    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载