Kodekraftt
/Blog
/Replace Each Space With A Character In A String In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Replace Each Space with a Character in a String in Java — Definition, Examples and Program

Learn how to replace each space in a string with a given character using Java. This blog explains the logic, examples, and a Java program without using replace() method.

Replace Each Space with a Character in a String in Java

Replacing spaces in a string is a common string manipulation task. It is often used in formatting text, URL encoding, and input normalization.

In this program, we replace every space character (' ') with a given character without using built-in string replacement methods.

How the Logic Works

  1. Convert the string into a character array.
  2. Traverse each character.
  3. If a space is found, replace it with the given character.
  4. Reconstruct the string.

Example 1

Input: "Java Programming"

Replace with: '-'

Output: Java-Programming

Example 2

Input: "Hello World Java"

Replace with: '*'

Output: Hello*World*Java

Java Program to Replace Each Space with a Character


import java.util.Scanner;

public class ReplaceSpace {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String str = sc.nextLine();

        System.out.print("Enter replacement character: ");
        char replaceChar = sc.next().charAt(0);

        char[] chars = str.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == ' ') {
                chars[i] = replaceChar;
            }
        }

        System.out.println("Modified String: " + new String(chars));
    }
}

Sample Output


Enter a string: Practice makes perfect
Enter replacement character: _
Modified String: Practice_makes_perfect

Important Notes

  • No replace() or replaceAll() method is used.
  • Only space characters are replaced.
  • Time complexity is O(n).

Practice Challenges

  1. Replace spaces with multiple characters like "%20".
  2. Replace only the first occurrence of space.
  3. Count how many spaces were replaced.

This problem helps in understanding character-level manipulation and safe string transformations in Java.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.