Compare the size of two strings str1 and str2

Source: Internet
Author: User

Write a function to compare the sizes of two strings str1 and str2. If they are equal, return 0. If str1 is greater than str2, return 1. If str1 is less than str2, return-1, do not call the character library function of the C ++/C string. Compile the strcmp function. The function is defined as intstrcmp (const char * src, const char * dst)

[Cpp]
# Include <stdio. h>
 
Int strcmp (const char * src, const char * dst)
{
Int I = 0;
While (src [I] & dst [I])
{
If (src [I]> dst [I])
Return 1;
Else
If (src [I] <dst [I])
Return-1;
Else
I ++;
}
Return 0;
}
Int main (int argc, char * argv [])
{
Char * str1 = "abc ";
Char * str2 = "abd ";
Printf ("% d \ n", strcmp (str1, str2 ));
Return 0;
}

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.