Implementation of C string manipulation function detailed parsing _c language

Source: Internet
Author: User

1. strlen (), Calculate string length

Copy Code code as follows:

int strlen (const char string)
{
int i=0;
while (String[i]) i++;
return i;
}

2. strcpy (), string copy.
Copy Code code as follows:

Char *strcpy (char *destination, const char *source)
{
while (*destinaton++=*source++);
return (destination-1);
}

3. Strcat (), string connection.
Copy Code code as follows:

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.
Copy Code code as follows:

int streql (char *str1,char *str2)
{
while ((*STR1==*STR2) && (*STR1))
{
str1++;
str2++;
}
Return ((*str1==null) && (*str2==null));
}

5. STRCHR (), find a character in the string.
Copy Code code as follows:

Char *strchr (const char *string,int letter)
{
while ((*string!=letter) & (*string))
string++;
return (string);
}

6. CHRCNT (), calculates the number of times a character appears in a string.
Copy Code code as follows:

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.
Copy Code code as follows:

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.