C# Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 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') + " "); 
3 6
2 5
3 5
2 6
3 7
Answer: Option
No answer description is available. Let's discuss.
12 comments Page 1 of 2.

Stephanie said:   7 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.

Mohd Asif said:   8 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.

SriHarsha said:   8 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.

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

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

Console.WriteLine (s1. Length);

Console.ReadLine ();

Output is => -1, 5.

Why -1, 5?

Chowdaiah said:   9 years 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.


So 2+5="25".

Raghvendra Singh Rajawat said:   10 years 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)

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.

Purusottam said:   1 decade ago
A String has a zero-based index,
So index of c is 2.

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

Sandeep said:   1 decade ago
If the index start in 0th element then it would be 24 how is 25 I can't understand.

Post your comments here:

Your comments will be displayed after verification.