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);
Discussion:
12 comments Page 1 of 2.
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.
If we replace s1.IndexOf('c') with s1.IndexOf('K') we get the result : 0 5.
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.
So length should be 4.
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.
s(1)=1, s(2)=c, s(3)=i, s(4)=t.
Total length=5. Since there are are 5 characters.
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?
Console.WriteLine (s1. IndexOf ('k'));
Console.WriteLine (s1. Length);
Console.ReadLine ();
Output is => -1, 5.
Why -1, 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".
Int+string=string.
So 2+5="25".
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)
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.
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers