Open In App

Java Program to find the Last Index of a Particular Word in a String

To find out the last occurrence of a character or string from a given string across which the substring or character is been searched over and returning the index value of the character or substring is found.

Real-life Example:



Consider an example of a book. The book contains pages where pages contain words. Now if a user wants to figure out the occurrence of a word of his own choice then the reader’s head rebound of question does that word to be searched even exist in the book And if yes, starts flipping out the pages prior to remembering the page where it last occurred because there may be chances that the word would repeat after the page which he updated the last and is done reading the book.  

Here the set of all the words in the book is referred to as ‘String’ and the word to be searched is referred to as ‘word’ itself. The exact place where the word occurred is referred to as ‘index’.



Now in Java, there is already an inbuilt method defined in strings over dealing with indexes in it known as indexOf(). It is used in finding out the index of a special character or sub-string itself from a given string. As usual, there are two possibilities, first is when a character or substring is present in the string it returns the index, and if not this function returns ‘-1’ if the character or substring is not present out in the string. -1 is returned in java as in java there is no such concept of negative indexed what is present therein python.

Similarly, there is also a predefined inbuilt function defined known as lastIndexOf() which specifies the last occurrence of a character or substring present in the string. The method starts working from the last of the string and moves backwards. Now if the ‘fromIndex is specified’ during the function call of lastIndexOf() method, then the search for a character or substring starts from the ahead of ‘frontIndex’ instead of the last of the string.

The directory in which inbuilt function is present:

java.util.String.lastIndexOf()

Return type:

Approach:

Example 1: Considering where the character or substring is not present in the string then it will return the last index value.




// Java Program to  find the Last
// Index of a particular word
// in a string
 
// Importing Classes/Files
import java.util.*;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // String in which char or
        // substring to be searched
        String str = "geeksforgeeks";
 
        // Substring to be searched
        String word = "geeks";
 
        // Printing last index of char
        // or substring is found
        // Printing -1 is not found
        System.out.println(str.lastIndexOf(word));
    }
}

Output
8

Time complexity: O(1)
Auxiliary Space: O(1)

Example 2: Considering where the character or substring is not present in the string then it will return a ‘-1’ value. 




// Java Program to  find the Last
// Index of a particular word
// in a string
 
// Importing Classes/Files
import java.util.*;
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // String in which char or
        // substring to be searched
        String str = "geeksforgeek";
 
        // Substring to be searched
        String word = "gfg";
 
        // Printing last index of char
        // or substring is found
        // Printing -1 is not found
        System.out.println(str.lastIndexOf(word));
    }
}

Output
-1

Time complexity: O(1)
Auxiliary Space: O(1)


Article Tags :