C# Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 9)
9.
Which of the following will be the correct output for the C#.NET code snippet given below?
String s1="Kicit";
Console.Write(s1.IndexOf('c') + " "); 
Console.Write(s1.Length);
3 6
2 5
3 5
2 6
3 7
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
12 comments Page 1 of 2.

Bibhuti said:   1 decade ago
I'm agree with @Purusottam when run, this time assign of address of "C" is 2 and total string address is 5 or length of index is 4, every compiler are start o to N if you assign this is another case.

Raghvendra Singh Rajawat said:   1 decade ago
indexOf is used to get the position of a particular character that is being checked. As we know that index always starts from 0 position that's why position of C will be 2 and length will b 5. (2,5)

Chowdaiah said:   1 decade ago
There is a string that's why zero based index will starts on 0 so 2 will come then after +" "means string how many characters are there 5 then finally.

Int+string=string.

So 2+5="25".

Mohd Asif said:   9 years ago
Hey guys I agree that index will be start from 0 then c is 2 but length should be 4 such as s1 (0) = k, s1 (1) = i, s1 (2) = c, s1 (3) = i, s1 (4) = t.

So length should be 4.

Rea said:   1 decade ago
Its 25 since the total number of letters is 5. we can not say letter 0 is(..), instead we start from letter 1 (which is k in this case) and say letter 1 is(..)

Arun said:   9 years ago
String s1 = "Kicit";

Console.WriteLine (s1. IndexOf ('k'));

Console.WriteLine (s1. Length);

Console.ReadLine ();

Output is => -1, 5.

Why -1, 5?

SriHarsha said:   9 years ago
Hey it is not like that. Here C value is = 2 because s(0) = k.

s(1)=1, s(2)=c, s(3)=i, s(4)=t.

Total length=5. Since there are are 5 characters.

Stephanie said:   8 years ago
Why? Strings are like Arrays and have a zero-based index.

If we replace s1.IndexOf('c') with s1.IndexOf('K') we get the result : 0 5.

Ravindra said:   1 decade ago
As you say it returns the position of first occurrence of substring. Answer would be 25 because index staring from 0 position.

Abhitesh Tyagi said:   1 decade ago
Answer would be 35.

Because IndexOf() returns the position of the first occurence of substring.


Post your comments here:

Your comments will be displayed after verification.