Replace Last Occurrence of Character in String Java

Replace Last Occurrence of Character in String Java | In this post, we aim to replace the last occurrence of the character in the string, to do this there are no direct methods or functions available in Java. Also see:- Find Second Occurrence of Character in String Java

Let us first understand the problem by some examples. An example of Java string replace last occurrence. 

String = “Hello”
Character to be replace = ‘l’
Character to pace = ‘L’

In the above-given string, the last occurrence of ‘l’ is the second occurrence at the 3rd position, which should be replaced by ‘L’. The result will be as shown below.

Result: HelLo

Now observe the below code. In order to replace last occurrence of character in string java, we have used a loop that iterates through the string from the last character to the first character of the string. Then if the character to be replaced is found then it replaces the character with the specified character. 

Program to Replace Last Occurrence of Character in String Java

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);

      System.out.print("Enter a String: ");
      String string = scan.nextLine();
      System.out.print("Enter a character to be replaced: ");
      char replace = scan.next().charAt(0);
      System.out.print("Enter a new character to place: ");
      char newChar = scan.next().charAt(0);

      // convert string to array of characters
      char[] charArr = string.toCharArray();
      for (int i = charArr.length - 1; i >= 0; i--) {
         if (charArr[i] == replace) {
            charArr[i] = newChar;
            break;
         }
      }

      // convert char array to string
      string = new String(charArr);

      System.out.println("After replacing the last occurrence " +  
                         "of \'" + replace + "\' with \'" + 
                         newChar + "\' = " + string);
      scan.close();
   }
}

Output:-

Enter a String: Program
Enter a character to be replaced: r
Enter a new character to place: X
After replacing the last occurrence of ‘r’ with ‘X’ = ProgXam

Enter a String: Java
Enter a Character to be replaced: J
Enter a new character to place: L
After replacing Last Occurrence of ‘J’ with ‘L’ = Lava

Test-case when a character doesn’t exist in the given string:-

Enter a String: I love Coding.
Enter a character to be replaced: P
Enter a new character to place: Q
After replacing the last occurrence of ‘P’ with ‘Q’ = I love Coding.

The logic of the code goes as follows:- We will iterate through the loop until the length of the string becomes 0. Then in the while loop if the character needed to replace is found then we replace it and come out of the loop. 

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.