w3resource logo
Java Programming Exercies

Java String Exercises: Replace all the 'd' characters with 'f' characters

Java String : Exercise-24 with Solution

Write a Java program to replace all the 'd' characters with 'f' characters.

Sample Solution:

Java Code:

public class Exercise24 {
    
   public static void main(String[] args)
    {
        String str = "The quick brown fox jumps over the lazy dog.";

        // Replace all the 'd' characters with 'f' characters.
        String new_str = str.replace('d', 'f');

        // Display the strings for comparison.
        System.out.println("Original string: " + str);
        System.out.println("New String: " + new_str);
    }
}

Sample Output:

Original string: The quick brown fox jumps over the lazy dog.                                                 
New String: The quick brown fox jumps over the lazy fog.

Java Code Editor:

public class Exercise24 {
    
   public static void main(String[] args)
    {
        String str = "The quick brown fox jumps over the lazy dog.";

        // Replace all the 'd' characters with 'f' characters.
        String new_str = str.replace('d', 'f');

        // Display the strings for comparison.
        System.out.println("Original string: " + str);
        System.out.println("New String: " + new_str);
    }
}

Improve this sample solution and post your code through Disqus