We can compare two strings to find whether two strings are equal or not. If they are not equal which comes first in the dictionary order.
So, to compare two strings; First, we need to keep compare on corresponding characters of the both strings till the corresponding characters are same and none of the string are exhausted. If both the strings are exhausted simultaneously, then the strings are identical i.e. equal; else strings are different.
So, to compare two strings; First, we need to keep compare on corresponding characters of the both strings till the corresponding characters are same and none of the string are exhausted. If both the strings are exhausted simultaneously, then the strings are identical i.e. equal; else strings are different.
If the strings are different, then we need to find out which strings comes first in the dictionary order. In that case, if the first strings is exhausted, then it comes first; else second string comes first;
If the strings are different and none of the strings are exhausted, then if the difference in the ASCII value of the corresponding characters is negative, i.e. character of the first string has lower ASCII value, then the first string comes earlier in the dictionary order. If the difference is positive, then the second string comes earlier in the dictionary order.
You can use built-in string comparison function 'strcmp()' to do the same. Look at the following code:
No comments:
Post a Comment