C language string operation function-Prototype

Source: Internet
Author: User

1. strcat


[Cpp]
Char * strcat (char * strDest, const char * strScr) // Add the source string to const, indicating that it is an input parameter
{
Char * address = strDest; // if this statement is placed after assert, a compilation error occurs.

Assert (strDest! = NULL) & (strScr! = NULL); // Add non-0 assertions to the source and target addresses

While (* strDest) // is while (* strDest! = '\ 0') Simplified Form
{// If while (* strDest ++) is used, an error occurs because ++ is not subject to loops.
StrDest ++; // restricted. Therefore, it must be in the body of the loop ++; because if * strDest refers
} // End sign '\ 0' to the string '.
 
While (* strDest ++ = * strScr ++) // while (* strDest ++ = * strScr ++ )! = '\ 0') Simplified Form
{
NULL; // you can use ++ in this loop condition,
} // The statement * strDest = '\ 0' can be added here. Is it necessary?

Return address; // return the destination address for chained operations
 
}

Char * strcat (char * strDest, const char * strScr) // Add the source string to const, indicating that it is an input parameter
{
Char * address = strDest; // if this statement is placed after assert, a compilation error occurs.

Assert (strDest! = NULL) & (strScr! = NULL); // Add non-0 assertions to the source and target addresses

While (* strDest) // is while (* strDest! = '\ 0') Simplified Form
{// If while (* strDest ++) is used, an error occurs because ++ is not subject to loops.
StrDest ++; // restricted. Therefore, it must be in the body of the loop ++; because if * strDest refers
} // End sign '\ 0' to the string '.

While (* strDest ++ = * strScr ++) // while (* strDest ++ = * strScr ++ )! = '\ 0') Simplified Form
{
NULL; // you can use ++ in this loop condition,
} // The statement * strDest = '\ 0' can be added here. Is it necessary?

Return address; // return the destination address for chained operations

}
2. strcmp


[Cpp]
Int _ cdecl strcmp (
Const char * src,
Const char * dst
)
{
Int ret = 0;

While (! (Ret = * (unsigned char *) src-* (unsigned char *) dst) & * dst)
++ Src, ++ dst;

If (ret <0)
Ret =-1;
Else if (ret> 0)
Ret = 1;

Return (ret );
}

Int _ cdecl strcmp (
Const char * src,
Const char * dst
)
{
Int ret = 0;

While (! (Ret = * (unsigned char *) src-* (unsigned char *) dst) & * dst)
++ Src, ++ dst;

If (ret <0)
Ret =-1;
Else if (ret> 0)
Ret = 1;

Return (ret );
}

 


[Cpp]
Int strcmp (const char * dest, const char * source)
{
Assert (NULL! = Dest) & (NULL! = Source ));
While (* dest & * source & (* dest = * source ))
{
Dest ++;
Source ++;
}
Return * dest-* source;
/* If dest> source, the return value is greater than 0. If dest = source, the return value is equal to 0. If dest <source, the return value is less than 0. */
}

Int strcmp (const char * dest, const char * source)
{
Assert (NULL! = Dest) & (NULL! = Source ));
While (* dest & * source & (* dest = * source ))
{
Dest ++;
Source ++;
}
Return * dest-* source;
/* If dest> source, the return value is greater than 0. If dest = source, the return value is equal to 0. If dest <source, the return value is less than 0. */
}

3. strcpy


[Cpp]
Char * strcpy (char * strDestination, const char * strSource)
{
Assert (strDestination! = NULL & strSource! = NULL );
Char * strD = strDestination;
While (* strD ++ = * strSource ++ )! = '\ 0 ');
Return strDestination;
}

Char * strcpy (char * strDestination, const char * strSource)
{
Assert (strDestination! = NULL & strSource! = NULL );
Char * strD = strDestination;
While (* strD ++ = * strSource ++ )! = '\ 0 ');
Return strDestination;
}

 

4. strncmp

[Cpp]
Char * strncpy (char * dest, const char * src, size_t count)
{
Char * tmp = dest;
Assert (src! = NULL & dest! = NULL );
While (count ){
If (* tmp = * src )! = 0)
Src ++;
Tmp ++;
Count --;
}
Return dest;
}

Char * strncpy (char * dest, const char * src, size_t count)
{
Char * tmp = dest;
Assert (src! = NULL & dest! = NULL );
While (count ){
If (* tmp = * src )! = 0)
Src ++;
Tmp ++;
Count --;
}
Return dest;
}


5. strrchr


[Cpp]
Char *
Strrchr (const char * s, int c)
{
Register const char * found, * p;
 
C = (unsigned char) c;
 
/* Since strchr is fast, we use it rather than the obvious loop .*/
 
If (c = '\ 0 ')
Return strchr (s, '\ 0 ');
 
Found = NULL;
While (p = strchr (s, c ))! = NULL)
{
Found = p;
S = p + 1;
}
 
Return (char *) found;
}

Char *
Strrchr (const char * s, int c)
{
Register const char * found, * p;

C = (unsigned char) c;

/* Since strchr is fast, we use it rather than the obvious loop .*/

If (c = '\ 0 ')
Return strchr (s, '\ 0 ');

Found = NULL;
While (p = strchr (s, c ))! = NULL)
{
Found = p;
S = p + 1;
}

Return (char *) found;
}

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.