Skip to content
Related Articles

Related Articles

Java Program to Separate the Individual Characters from a String
  • Difficulty Level : Basic
  • Last Updated : 27 Jan, 2021
GeeksforGeeks - Summer Carnival Banner

The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it’s content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided as the input. Therefore, to separate each character from the string, the individual characters are accessed through its index.

Examples :

Input : string = "GeeksforGeeks"
Output: Individual characters from given string :
         G e e k s f o r G e e k s
Input : string = "Characters"
Output: Individual characters from given string :
         C h a r a c t e r s

Approach 1:

  1. First, define a string.
  2. Next, create a for-loop where the loop variable will start from index 0 and end at the length of the given string.
  3. Print the character present at every index in order to separate each individual character.
  4. For better visualization, separate each individual character by space.

Below is the implementation of the above approach :

Java




// Java Program to Separate the
// Individual Characters from a String
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        String string = "GeeksforGeeks";
 
        // Displays individual characters from given string
        System.out.println(
            "Individual characters from given string: ");
 
        // Iterate through the given string to
        // display the individual characters
        for (int i = 0; i < string.length(); i++) {
            System.out.print(string.charAt(i) + " ");
        }
    }
}

 
 



Output
Individual characters from given string: 
G e e k s f o r G e e k s 

Time Complexity: O(n), where n is the length of the string. 

Approach 2:

  1. Define a String.
  2. Use the toCharArray method and store the array of Characters returned in a char array.
  3. Print separated characters using for-each loop.

Below is the implementation of the above approach :

Java




// Java Implementation of the above approach
 
import java.io.*;
 
class GFG
{
    public static void main(String[] args)
    {
         // Initialising String
        String str = "GeeksForGeeks";
 
        // Converts the string into
         // char array
        char[] arr = str.toCharArray();
 
        System.out.println(
            "Displaying individual characters"
             + "from given string:");
       
        // Printing the characters using for-each loop
        for (char e : arr)
            System.out.print(e + " ");
    }
}
Output
Displaying individual charactersfrom given string:
G e e k s F o r G e e k s 

Time Complexity: O(N)
Space Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :