✏️ Explanatory Question
[String]
Question:
Write the Java statement to print the position of the 2nd occurrence of the letter 'I' within the word "MICHIGAN".
[String]
Question:
Write the Java statement to print the position of the 2nd occurrence of the letter 'I' within the word "MICHIGAN".
Answer:
System.out.println("MICHIGAN".indexOf('I', 2));
Explanation:
The method:
indexOf(character, startingIndex)
is used to search for a character starting from a specific position.
In the word:
MICHIGAN
Character positions are:
M I C H I G A N
0 1 2 3 4 5 6 7
The first occurrence of 'I' is at index:
1
The second occurrence of 'I' is at index:
4
Therefore, the output will be:
4