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)