Algorithm C language string functions implemented 2

1.strcpy

用 法: char * strcpy (char * str1, char * str2);

Function: copy string, to assign str2 in str1

* myStr cahr ( char * str1, cahr * str2) 
{ 
    char * P = str1;
     IF (str2 str1 == NULL || == NULL)
         return NULL;
     the while (! * str2 = NULL) // copy the characters * str2 to terminate ( '\ 0' corresponds to NULL) 
    {
         * * str1 = str2; 
        str1 ++ ; 
        str2 ++ ; 
    }
     * str1 = ' \ 0 ' ; // because a copy of the last character '\ 0', so the exit loop, so make the 
    return P; // return the first address of str1 
}

 

2strset
yong fa: for char * strset (for char * the str, c are for char);

Function: all characters in a string are set to specify the character

char *mystrset(char *str,char ch)
{
    char *p=str;
    while(*str!='\0')
    {
        *str=ch;
        str++;
    }
    return p;
}

 

3. strstr
用  法: char *strstr(char *str1, char *str2);

Function: Find string str1, string str2 to see whether or not included in the str1. If there is return to the position of the first occurrence of the string str2, otherwise empty

char * mystrstr ( char * str1, char * str2) 
{ 
    char * P = NULL;
     IF (str2 str1 == NULL || == NULL)
         return P;
     int length1 = strlen (str1); // master string length 
    int = strlen length2 (str2); // substring length 
    for ( int I = 0 ; I <length1-length2; I ++ ) 
    { 
        int In Flag = . 1 ; // assumed to be equal string 
        for ( int J = 0 ; J <length2 ; J ++ ) 
        {
            IF (! STR [I + J] STR = [J]) // long as a character is not equal to the Release cycle 
            { 
                In Flag = 0 ;
                 BREAK ; 
            } 
        } 
        IF (In Flag == . 1 ) 
        { 
            return (str1 + I); / / return position found 
            BREAK ; 
        } 
    } 
    return P; 
}

Speaking today, would like to explore the algorithm, there is a message or better under the program!

Guess you like

Origin www.cnblogs.com/zulkar/p/10973840.html