• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
yuzaipiaofei
博客园    首页    新随笔    联系   管理    订阅  订阅

字符串操作函数的几个基本函数

//---------------------------------------------------------------------------
int strlen (const char *s)
{
    const char *sc;

    for (sc = s; *sc != '\0'; ++sc)
    {
    }
    return sc - s;
}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int strcmp (const char *cs, const char *ct)
{
    signed char __res;

    while (1)
    {
        if ((__res = *cs - *ct++) != 0 || !*cs++)
            break;
    }
    return __res;
}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int strncmp (const char *cs, const char *ct, int count)
{
    signed char __res = 0;

    while (count) 
    {
        //dbg_print("*cs = %c, *ct = %c, count = %d\n", *cs, *ct, count);
        if ((__res = *cs - *ct++) != 0 || !*cs++)
            break;
        count--;
    }
    return __res;
}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void * memset (void *s, int c, int count)
{
    char *xs = s;

    while (count--)
        *xs++ = c;
    return s;
}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void * memcpy (void *dest, const void *src, int count)
{
    char *tmp = dest;
    const char *s = src;

    while (count--)
        *tmp++ = *s++;
    return dest;
}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int memcmp (const void *cs, const void *ct, int count)
{
    const unsigned char *su1, *su2;
    int res = 0;

    for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
        if ((res = *su1 - *su2) != 0)
            break;
    return res;
}

//---------------------------------------------------------------------------

posted @ 2012-03-08 14:28  yuzaipiaofei  阅读(196)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3