Java Program to Replace Last Character Occurrence in a String

Write a Java Program to replace Last Character Occurrence in a String with an example. In this Java replace the last occurrence of a string character example, we used the toCharArray() to convert the repstCharStr to replastCharArr char array. 

Next, we used a While loop to iterate the repstCharStr string characters from end to start (reverse). Within the loop, we are comparing each character in the replastCharArr array with the replace_ch. If they are equal, we assigned new_ch in that replastCharArr index position followed by break statement to exit from the While loop.

import java.util.Scanner;

public class ReplaceLastCharOccur1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String repstCharStr;
		char replace_ch, new_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		char[] replastCharArr = repstCharStr.toCharArray();
		int i = replastCharArr.length - 1;
		while(i >= 0)
		{
			if(replastCharArr[i] ==  replace_ch) {
				replastCharArr[i] = new_ch;
				break;
			}
			i--;
		}
		System.out.format("\nAfter replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(replastCharArr);
	}
}

Java Replace Last Character Occurrence in a String output

Enter a String to replace last Char Occurrence =  java

Enter a Character to Replace =  a

Enter a New Character =  T

After replacing Last Occurrence of a with T = javT

This Java replace the last occurrence of a character example is the same as the above, and we replaced the While loop with For Loop.

import java.util.Scanner;

public class ReplaceLastCharOccur2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String repstCharStr;
		char replace_ch, new_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		char[] replastCharArr = repstCharStr.toCharArray();
		
		for(int i = replastCharArr.length - 1; i >= 0; i--)
		{
			if(replastCharArr[i] ==  replace_ch) {
				replastCharArr[i] = new_ch;
				break;
			}
		}
		System.out.format("\nAfter replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(replastCharArr);
	}
}
Enter a String to replace last Char Occurrence =  tutorial

Enter a Character to Replace =  t

Enter a New Character =  M

After replacing Last Occurrence of t with M = tuMorial

Java Program to Replace Last Character Occurrence in a String Example 3

In Java Programming, we used the lastIndexOf to get the last index position of replace_ch in repstCharArr. Next, we assigned the new_ch at that position.

import java.util.Scanner;

public class ReplaceLastCharOccur3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String repstCharStr;
		char replace_ch, new_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		char[] repstCharArr = repstCharStr.toCharArray();;
		int i = repstCharStr.lastIndexOf(replace_ch);
		repstCharArr[i] = new_ch;

		System.out.format("\nAfter replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(repstCharArr);
	}
}
Java Program to Replace Last Character Occurrence in a String 3

We can use the Java String substring function to replace the last character occurrence. In this Java program, repstCharStr.lastIndexOf(replace_ch) finds the last index position. Next, repstCharStr.substring(0, i) returns substring up to i. Next, we added the new_ch to it. Then, we used another substring repstCharStr.substring(i + 1, repstCharStr.length() to concat string from i to end of that string.

import java.util.Scanner;

public class ReplaceLastCharOccur4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String repstCharStr;
		char replace_ch, new_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		int i = repstCharStr.lastIndexOf(replace_ch);
		String out = repstCharStr.substring(0, i) + new_ch + repstCharStr.substring(i + 1, repstCharStr.length() );

		System.out.format("\nAfter replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(out);
	}
}
Enter a String to replace last Char Occurrence =  tutorial gateway

Enter a Character to Replace =  t

Enter a New Character =  X

After replacing Last Occurrence of t with X = tutorial gaXeway

Java Program to Replace Last Character Occurrence in a String using StringBuilder

The Java StringBuilder also has the sb.lastIndexOf(String.valueOf(replace_ch)) but it accepts the string value so that we converted replace_ch to string. Next, deleteCharAt function removes the character. Then, sb.insert(j, new_ch) function insert the new_ch character at that position.

import java.util.Scanner;

public class ReplaceLastCharOccur5 {
	private static Scanner sc;
	public static void main(String[] args) {
		String repstCharStr;
		char replace_ch, new_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(repstCharStr);
		//int i = repstCharStr.lastIndexOf(replace_ch);
		int j = sb.lastIndexOf(String.valueOf(replace_ch));
		sb.deleteCharAt(j);
		sb.insert(j, new_ch);

		System.out.format("\nAfter "
				+ "replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(sb);
	}
}
Enter a String to replace last Char Occurrence =  java programming

Enter a Character to Replace =  a

Enter a New Character =  L

After replacing Last Occurrence of a with L = java progrLmming

The Java StringBuffer also has the lastIndexOf, deleteCharAt, insert functions. So, we used those functions to replace the last occurrence of a character in a string.

import java.util.Scanner;

public class ReplaceLastCharOccur6 {
	private static Scanner sc;
	public static void main(String[] args) {
		String repstCharStr;
		char replace_ch, new_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		StringBuffer sbuff = new StringBuffer(repstCharStr);
		//int i = repstCharStr.lastIndexOf(replace_ch);
		
		int j = sbuff.lastIndexOf(String.valueOf(replace_ch));
		sbuff.deleteCharAt(j);
		sbuff.insert(j, new_ch);

		System.out.format("\nAfter "
				+ "replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(sbuff);
	}
}
Enter a String to replace last Char Occurrence =  hello everyone

Enter a Character to Replace =  e

Enter a New Character =  W

After replacing Last Occurrence of e with W = hello everyonW