Implementation of C string processing functions

Source: Internet
Author: User

1. strlen (), calculate the string length

Int strlen (const char string)

{

Int I = 0;

While (string [I]) I ++;

Return I;

}

2. strcpy (), copy the string.

 

Char * strcpy (char * destination, const char * source)

{

While (* destinaton ++ = * source ++ );

Return (destination-1 );

}

3. strcat (), string connection.

 

Char * strcat (char * target, const char * source)

{

Char * original = target;

While (* target) target ++; // Find the end of the string

While (* target ++ = * source ++ );

Return (original );

}

4. streql () to determine whether two strings are equal.

 

Int streql (char * str1, char * str2)

{

While (* str1 = * str2) & (* str1 ))

{

Str1 ++;

Str2 ++;

}

Return (* str1 = NULL) & (* str2 = NULL ));

}

5. strchr (): searches for a character in the string.

 

Char * strchr (const char * string, int letter)

{

While (* string! = Letter) & (* string ))

String ++;

Return (string );

}

6. chrcnt () is used to calculate the number of times a character appears in a string.

Int chrcnt (const char * string, int letter)

{

Int count = 0;

While (* string)

If (* string = letter) count ++;

Return count;

}

7. strcmp () to determine whether two strings are equal.

Int strcmp (const char * str1, const char * str2)

{

While (* str1 = * str2) & (* str1 ))

{

Str1 ++;

Str2 ++;

}

If (* str1 = * str2 )&&(! * Str1) // Same strings

Return o;

Else if (* str1 )&&(! * Str2) // Same but str1 longer

Return-1;

Else if (* str2 )&&(! * Str1) // Same but str2 longer

Else

Return (* str1> * str2 )? -1:1 );

}

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.