实现自己的mystring.h

把常用的字符串处理函数写了一遍,没有考虑地址重叠。

/**************************************************/

char* mystrcpy( char *destin, const char *source )
{
    if( destin == NULL ||  source == NULL )
    {
        return NULL;
    }
    
    char *addr = destin;

    while( ( *destin++ = *source++ )  != '\0' );

    return addr;
}


/**************************************************/

int mystrlen( const char *source )
{
    if( source == NULL  )
    {
        return 0;
    }

    int count = 0;

    while( *source++ != '\0' )
    {
        count++;
    }

    return count;
}
 

/**************************************************/

char* mystrncpy( char *destin,const char *source, int num )
{
    if( destin == NULL ||  source == NULL )
    {
        return NULL;
    }
    
    int limit = mystrlen( source );
    char *addr = destin;
    
    if( num > limit )
    {
        return NULL;
    }
    else
    {
        while( num-- )
	{
	    *destin++ = *source++;
	}
        
        *destin = '\0';

	return addr;
    }
}

/**************************************************/

char* mystrcat( char *destin, const char *source ) 
{
    if( destin == NULL ||  source == NULL )
    {
        return NULL;
    }

    char *addr = destin; 

    while( *destin++ != '\0' );

    destin--;

    while( ( *destin++ = *source++ )  != '\0' );

    return addr;
}

/**************************************************/

char* mystrchr( char *source, char ch )
{
    if( source == NULL )
    {
        return NULL;
    }

    while( *source != '\0' && ( *source++ != ch ) );
    
    return *source == '\0' ? NULL:--source;
}

/**************************************************/

int mystrcmp( const char *destin, const char *source )
{
    if( destin == NULL ||  source == NULL )
    {
        return destin - source;
    }
    
    while( *destin && *source && ( *destin++ == *source++ ) );
    
    return *( destin -1 ) != *( source -1 ) ? \
    *( destin-1 ) - *( source-1 ) : *destin - *source;
}

/**************************************************/

int mystrncmp( const char *destin, const char *source, int num )
{
    if( destin == NULL ||  source == NULL || num <= 0 )
    {
        return destin - source;
    }
 
    while( num-- > 0 && *destin && *source && ( *destin++ == *source++ ) );

    return  *( destin-1 ) != *( source-1 ) || \
            ( num == 0 && *destin && *source )  ? \
    *( destin-1 ) - *( source-1 ) : *destin - *source;        
//考虑当进行最后一次比较时,如果字符串有一个为结束符,那么实际没有进行++
}

/**************************************************/

char* mystrstr( const char *source, const char *destin )
{
    if( destin == NULL || source == NULL || \
    *destin == '\0' || *source == '\0' )
    {
        return NULL;
    }
    
    char *position = (char *)source;
    char *head = (char *)destin;

    while( *source )
    {
        position = (char *)source;
	    destin = head;

        while( *destin && *source && *destin++ == *source++ );
        
        if( *destin == '\0' && *( destin-1 ) == *( source-1 ) )
	    {
	    return position;
	    }

	    if( *source == '\0' )
	    { 
	        return NULL;
    	}

        source = position + 1 ;
    }
}


 

你可能感兴趣的:(写的程序)