Home page

Wednesday, May 29, 2024

Find first and last occurrence of the letter ‘s’ in samosa with index number

public class Main { 
 public static void main(String[] args) { 
     String str = "samosa"; 
     int firstIndex = str.indexOf('s'); 
     int lastIndex = str.lastIndexOf('s'); 

    System.out.println("First occurrence of 's' in " + str + " is at index " + firstIndex); 
    System.out.println("Last occurrence of 's' in " + str + " is at index " + lastIndex); 
     
}

No comments:

Post a Comment