Find the position of the substring in a string. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Find the position of the substring in a string.

If there are more than one substrings available then return the position for the last one. Here position means character position of the first character of substring. Input Format: First line contains a string. Second line contains substring. I/P Hello world Hello reader. Hello O/P 13

7th Oct 2017, 7:14 AM
Anuj Khetan
Anuj Khetan - avatar
2 Answers
+ 6
Here is in Java: System.out.println(string.lastIndexOf(substring));
9th Oct 2017, 10:17 AM
LukArToDo
LukArToDo - avatar
+ 1
You can use the str.find(substr) method to find the position of the first one, and then remove that substring to find the next one until there are no other substrings. In each iteration you have to save the number of characters you concatenate or you'll get wrong results!
9th Oct 2017, 5:01 PM
Dion