| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1908 人关注过本帖
标题:怎么写高效率的字符串处理函数
只看楼主 加入收藏
cs1357
Rank: 1
等 级:新手上路
帖 子:2
专家分:5
注 册:2009-9-10
收藏
得分:0 
int Strlen(const char *str)
{
   assert(*str != NULL);
   int icount = 0;
   while(*str++)
   {
      icount++;
   }
   return icount;
}
2009-09-27 13:41
lhl198989
Rank: 2
等 级:论坛游民
帖 子:16
专家分:67
注 册:2009-9-12
收藏
得分:0 
试一下这个
size_t strlen(register const char *str)
{
  size_t cnt=0;
  while(*str!='\0')
     ++cnt;
  return cnt;
}
2009-09-27 23:38
lhl198989
Rank: 2
等 级:论坛游民
帖 子:16
专家分:67
注 册:2009-9-12
收藏
得分:0 
忘了,少写了一句话。
size_t  str_len( register const char *str)
{
  size_t  cnt=0;
  for( ; *str!='\0' ; ++str )
      ++cnt ;
  return  cnt ;
}
2009-09-28 00:54
lansong
Rank: 4
等 级:业余侠客
帖 子:79
专家分:226
注 册:2009-6-11
收藏
得分:0 
以下是引用cs1357在2009-9-27 13:41:59的发言:

int Strlen(const char *str)
{
   assert(*str != NULL);
   int icount = 0;
   while(*str++)
   {
      icount++;
   }
   return icount;
}
assert(*str != NULL);//应该为assert(str != NULL);传入的不能使NULL指针,但是可以*str可以是NULL
2009-09-28 01:13
findugs
Rank: 1
等 级:新手上路
帖 子:10
专家分:7
注 册:2009-9-24
收藏
得分:0 
int mystrlen(char *mystr)
{
    int sum=0;
    while(mystr[sum++])
    ;
    return --sum;
}
2009-09-28 16:44
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
看看BSD的实现

 1 /*-
    2  * Copyright (c) 1990, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  * $FreeBSD: src/sys/libkern/strlen.c,v 1.6 1999/08/28 00:46:37 peter Exp $
   34  * $DragonFly: src/sys/libkern/strlen.c,v 1.5 2004/01/26 11:09:44 joerg Exp $
   35  */
   36
   37 #include <sys/libkern.h>
   38
   39 size_t
   40 strlen(const char *str)
   41 {
   42         const char *s;
   43
   44         for (s = str; *s; ++s);
   45         return(s - str);
   46 }

without further ado, let’s get started
2009-09-28 16:51
快速回复:怎么写高效率的字符串处理函数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012260 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved