C# Programming - Strings - Discussion
Discussion Forum : Strings - General Questions (Q.No. 13)
13.
Which of the following will be the correct output for the C#.NET code snippet given below?
String s1 = "Five Star";
String s2 = "FIVE STAR";
int c;
c = s1.CompareTo(s2);
Console.WriteLine(c);
Discussion:
11 comments Page 1 of 2.
Sudarshan said:
1 decade ago
As we know that compareTo() compares the current instance with another instance.
int c;
c = s1.CompareTo(s2);
s1<s2 then -1.
s1>s2 then 1.
s1 == s2 then 0.
int c;
c = s1.CompareTo(s2);
s1<s2 then -1.
s1>s2 then 1.
s1 == s2 then 0.
(1)
Vaishnavi.K said:
1 decade ago
Compare the ASCII value of I and i.
Mandar said:
1 decade ago
How can possible that answer please send me reply.
Abhitesh tyagi said:
1 decade ago
As we know that compareTo() compares the current instance with another instance // int n=string.compareTo(s1,s2);
the compare method performs a case sensitive comparison and returns different integer values for different conditionsas under:
. 0 ,if s1 = s2
. 1 ,if s1>s2
. -1 ,if s1<s2
For example if s1="abc" and s2="ABC"
Then n will be assigned a value of -1.because a lowercase letter has a smaller ASCII value than an uppercase letter.
the compare method performs a case sensitive comparison and returns different integer values for different conditionsas under:
. 0 ,if s1 = s2
. 1 ,if s1>s2
. -1 ,if s1<s2
For example if s1="abc" and s2="ABC"
Then n will be assigned a value of -1.because a lowercase letter has a smaller ASCII value than an uppercase letter.
Kanika Kapoor said:
1 decade ago
If the first string is bigger, the result is 1. If both strings are equal, the result is 0. If the first string is smaller, the result is -1.
Rajkumar said:
1 decade ago
No we know that ascii value of A TO Z is 65 to 90 and Ascii value of a To z is 97 to 122 ascii value of I is < i 73<105 so result is 1
Uttam Jaiswal said:
1 decade ago
Correct answer is -1 because CompareTo sorts data in ascending order and string s2 comes first with higher value.
Anurag said:
1 decade ago
compareTo method in string class used to compare two strings if both were matching it will return one otherwise will return -1.
Arun gupta said:
1 decade ago
Answer is -1 because CompareTo sorts data in ascending order and string s2 comes first with higher value.
0, if s1=s2.
1, if s1>s2.
-1, if s1<s2.
0, if s1=s2.
1, if s1>s2.
-1, if s1<s2.
Soniya said:
1 decade ago
I have one doubt, suppose it is comparing ASCII value of both string.
Then s1 will have greater ASCII value then s2.
So answer will, s1>s2 then 1.
Please give me solution.
Then s1 will have greater ASCII value then s2.
So answer will, s1>s2 then 1.
Please give me solution.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers