How to compare strings in C language and how to calculate the length of strings

Source: Internet
Author: User

/*************************************** ******************************
* Author: Samson
* Date: 01/09/2012
* Test Platform:
* GNU Linux version 2.6.29.4
* GCC version 4.4.0 20090506 (Red Hat 4.4.0-4) (GCC)

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

/* Compare two strings */
Int _ strcmp (const char * S1, const char * S2)
{
While (* S1! = '\ 0' & * S2! = '\ 0 ')
{
If (* S1! = * S2)
{
Return * S1-* S2;
}
S1 ++;
S2 ++;
}
Return 0;
}

/* Compare two strings */
Int _ strncmp (const char * S1, const char * S2, int N)
{
While (* S1! = '\ 0') & (* S2! = '\ 0') & (n> 0 ))
{
If (* S1! = * S2)
{
Return * S1-* S2;
}
S1 ++;
S2 ++;
N --;
}
Return 0;
}

/* Calculate the length of a string */
Int _ strlen (const char * s)
{
Int Len = 0;

While ('\ 0 '! = * S ++)
{
Len ++;
}
Return Len;
}

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.