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);
0
1
2
-1
-2
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
11 comments Page 1 of 2.

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.

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.

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.
(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.

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

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.

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.

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.

Arvind said:   10 years ago
I think same @Soniya and my question is same to you?

Please any one give answer.

Mandar said:   1 decade ago
How can possible that answer please send me reply.


Post your comments here:

Your comments will be displayed after verification.