strtok函数的实现

上午回帖子---“C语言操作文件”,用到了strtok这个函数,这里看一下它的简单实现。

 

char *mystrtok(char *s,const char *delim) 
{
    
static char *last;
    
char *tok;
    
char *ucdelim;
    
char *spanp;
    
int c,sc;

    
/*s为空,并且上次剩余值也为空,则直接返回NULL,否则s为last或当前值中有值的一方*/
    
if (s == NULL && (s = last) == NULL)
        
return NULL;
  
    
    
int found = 0;//是否找到与delim匹配的字符
    
    
//处理连续的待匹配的字符
    cont:
    c
=*s++;
    
for (spanp = (char *)delim;(sc = *spanp++!= 0;)
    
{
        
if (c == sc)
            
goto cont;
    }

    
if (c == 0
    
{
        last 
= NULL;
        
return NULL;
    }


    tok 
= s-1;
    
while (!found && *!= '\0'
    
{
        ucdelim 
= (char *) delim;
        
while (*ucdelim) 
        
{
            
if (*== *ucdelim) 
            
{
                found 
= 1;
                
*= '\0';
                last 
= s + 1;
                
break;
            }

            ucdelim
++;
        }

        
if (!found)
        
{
            s
++;
            
if(*s=='\0')
                last 
= NULL;
        }

    }


    
return tok;
}

 

posted @ 2009-05-29 17:19  回忆1919  阅读(4010)  评论(0编辑  收藏  举报