Java Program to Find Last Character Occurrence in a String

Write a Java Program to Find Last Character Occurrence in a String with an example. In this java Last Character Occurrence example, we used the While loop to iterate the lastCharStr from start to end. We used the String charAt function on lastCharStr to get the character at each index position within the while loop. 

Next, we are comparing that character with the lsch character to check whether they are equal. If True, we assigned the i value to the index variable and incremented the i value. Next, we used the Java If Else statement to check the index value equals -1 (no match). If it is False, then print the character index position.

import java.util.Scanner;

public class LastCharOccurence1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lastCharStr;
		int i = 0, lIndex = -1;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to find Last Occurrence =  ");
		lastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Last Character to Find =  ");
		int lsch = sc.next().charAt(0);
		
		while(i < lastCharStr.length())
		{
			if(lastCharStr.charAt(i) ==  lsch) {
				lIndex = i;
			}
			i++;
		}
		if(lIndex == -1) {
			System.out.println("\nSorry! We haven't found the Character ");
		}
		else {
			System.out.format("\nThe Last Character Occurrence of %c at %d position", lsch, lIndex);
		}
	}

}

Java Last Character Occurrence in a String output

Please Enter String to find Last Occurrence =  java programmings

Enter the Last Character to Find =  a

The Last Character Occurrence of a at 10 position

Java Program to Find Last Character Occurrence in a String Example 2

It is the same Java Last Character Occurrence example as the above, and we replaced the While loop with For Loop.

import java.util.Scanner;

public class LastCharOccurence2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lastCharStr;
		int i, lIndex = 0;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String for Last Occurrence =  ");
		lastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Last Character to Find =  ");
		char lsch = sc.next().charAt(0);
		
		for(i = 0; i < lastCharStr.length(); i++)
		{
			if(lastCharStr.charAt(i) ==  lsch) {
				lIndex = i;
			}
		}
		if(lIndex == -1) {
			System.out.println("\nSorry! We haven't found the Character ");
		}
		else {
			System.out.format("\nThe Last Character Occurrence of %c at %d position", lsch, lIndex );
		}
	}

}
Please Enter String to find Last Occurrence =  learn java

Enter the Last Character to Find =  a

The Last Character Occurrence of a at 9 position

In Java Programming, there is a string function called lastIndexOf. This lastIndexOf function returns the last index position of the lsch character in a string. Next, we used the string charAt function on lastCharStr to return the character at that index position.

import java.util.Scanner;

public class LastCharOccurence3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lastCharStr;
		char lsch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to find Last Occurrence =  ");
		lastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Last Character to Find =  ");
		lsch = sc.next().charAt(0);
		
		int x = lastCharStr.lastIndexOf(lsch);
		
		System.out.format("\nThe Last Character Occurrence of %c at %d position", 
				lastCharStr.charAt(x), x);
	}
}
Java Program to Find Last Character Occurrence in a String 3

The Java StringBuilder and StringBuffer also have the lastIndexOf and charAt function to return the string’s Last character occurrence. In this Java example code, we converted the given string to StringBuilder, StringBuffer, and returns the output.

import java.util.Scanner;
public class LastCharOccurence4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lastCharStr;
		char ch;		
		sc= new Scanner(System.in);
		System.out.print("\nPlease Enter String to find Last Occurrence =  ");
		lastCharStr = sc.nextLine();		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(lastCharStr);
		int a = sb.lastIndexOf(String.valueOf(ch));
		
		System.out.format("\nThe Last Character Occurrence of %c at %d position", 
				sb.charAt(a), a);
		
		StringBuffer sbuff = new StringBuffer(lastCharStr);
		int b = sb.lastIndexOf(String.valueOf(ch));
		
		System.out.format("\nThe Last Character Occurrence of %c at %d position", 
				sbuff.charAt(b), b);
	}
}
lease Enter String to find Last Occurrence =  hello world l

Enter the Character to Find =  l

The Last Character Occurrence of l at 12 position
The Last Character Occurrence of l at 12 position

Please refer to the following string programs.

  1. Count the Total Occurrence of a Character in a String
  2. Find All Occurrences of a Character in a String
  3. First Character Occurrence in a String
  4. Replace First Character Occurrence in a String
  5. Remove First Character Occurrence in a String
  6. Replace Last Character Occurrence in a String
  7. Remove the Last Character Occurrence in a String
  8. First and Last Character in a String
  9. Remove The First and Last Character in a String
  10. Maximum Occurring Character in a String
  11. Minimum Occurring Character in a String
  12. Frequency of each Character in a String
  13. Remove All Occurrences of a Character in a String