+ 5
On what basis comparison is done in strings?
I was writing codes with comparison of strings(words or sentences) and I was unable to understand, on what basis the interpreter was comparing the strings.
6 Respostas
+ 5
Here is an ASCII table for your reference:
http://www.asciitable.com/mobile/
0
~ swim ~ Thanks. Very helpful
- 1
Kamal Jain
Default comparison in case sensitive languages is done using lexographic ordering of characters i.e that is the position of characters in the Ascii Table. If the compared character match then string length is taken into account. Some languages may have additional criteria. so in str1 = "abcB"
str2 = "abcA", str2 is less than str1 because char 'A' comes before char 'B'.
str3 = "abcBb" between str1 and str3, the compared characters matches but str3 is bigger lengthwise than str1, hence it is bigger.
Non case sensitive languages ignores the position of upper case and lowercase characters in Ascii table and sort on alphbetical order.
In alphanumeric string like
str4="Bdc5E" and str5= "Bdc4E",
str5 is less than str4 because 4 comes before 5.
This is mostly the case with compare function in almost all languages (i do not know of the exception) String compare functions returns
0 if first string equals second string
>0 if first string greater than second string
<0 if first string less than second string.
If you do not understand string comparison, then on what basis were you writing the code? What was your expectation?
An advice - read the theory first before doing the practicals !
- 2
hi