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.
Abhitesh Tyagi said:
1 decade ago
Answer would be 35.
Because IndexOf() returns the position of the first occurence of substring.
Because IndexOf() returns the position of the first occurence of substring.
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.
Sandeep said:
1 decade ago
If the index start in 0th element then it would be 24 how is 25 I can't understand.
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(..)
Purusottam said:
1 decade ago
A String has a zero-based index,
So index of c is 2.
So index of c is 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".
Int+string=string.
So 2+5="25".
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?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers