Character array (judging string size)

Source: Internet
Author: User
Tags first string


Reads two strings from the keyboard, compares the size of the string
Idea:1> string longer than string short
2> when two strings are the same length, starting from the first bit until the size is judged
3> If the size is not determined then the string is equal
Char ch1[100];
Char ch2[100];
printf ("Please enter the first string: \ n");
gets_s (CH1);
printf ("Please enter a second string: \ n");
gets_s (CH2);
int ch1len = strnlen_s (ch1, sizeof (CH1));//First length
int ch2len = strnlen_s (CH2, sizeof (CH2));//Second length
if (Ch1len > Ch2len)
{
printf ("ch1:%d greater than ch2:%d", CH1, CH2);//If the character 1 is greater than the character 2 then output c1>c2
}
else if (Ch1len<ch2len)
{
printf ("ch1:%d less than ch2:%d", CH1, CH2);//If the character 2 is greater than the character 1 then output c1<c2
}
else if (Ch1len = = Ch2len)
{
int a=strcmp (CH1, CH2);
if (a > 0)
{
printf ("ch1:%d greater than ch2:%d", CH1, CH2);
}
else if (a<0)
{
printf ("ch1:%d less than ch2:%d", CH1, CH2);
}
Else
{
printf ("ch1:%d equals ch2:%d", CH1, CH2);
}
}

Character array (judging string size)

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.