Java Program to Separate the Individual Characters from a String


In the field of computer science, the string is a collection of a continuous character data sets. Here in this particular problem we will use some spaces to execute the logic. The individual characters present here in the string can be accessed through its index to perform the code.

Here is a general flow to build a logic code to separate the individual characters from a string in a Java environment.

Define a string with characters.
Print the individual characters present from a given string.
Set an initial to zero.
Print the string by using the function string.charAt(i).
Iterate the method with i=i+1.

In a java environment, the strings have the immutable in java. It means if one object is created in a string, it is impossible to change further. To read some characters in Java, we use a method called next() which follow the method charAt(0). The next() function returns the next token value as an output string and chatAt() function usually returns the first character in tht particular string.

Algorithm to separate the individual characters from a string

Here is the algorithm to separate the individual characters from a string in a Java environment.

  • Step 1 − Start.

  • Step 2 − Define a string for the method.

  • Step 3 − Define a for-loop.

  • Step 4 − The loop variable will start from 0.

  • Step 5 − The loop will end ath the length of a string.

  • Step 6 − Separate each and every character.

  • Step 7 − Print the characters present at every index.

  • Step 8 − Separate each and every character by using space present in the string.

  • Step 9 − Terminate.

Syntax to separate the individual characters from a string

public class DefineIndividualCharactersWithUs {  
   public static void main(String[] args) {  
      String string = "characters";
      System.out.println("Individual characters from given string. Please have a look:");
      for(int a = 0; a < string.length(); a++){  
         System.out.print(string.charAt(a) + "  ");  
      }  
   }  
} 

Here in this syntax above, we display the individual characters from a particular given string. Then iterate the string and display it.

Approach

  • Approach 1 − Separate the individual characters from a string by using a for loop.

  • Approach 2 − Separate the individual characters from a string by using toCharArray method and a for-each loop

Separate the individual characters from a string by using a for loop

To separate the individual characters from a string by using a for loop, we need to follow some step by step process –

  • Start.

  • Define a string.

  • Declare a for-loop.

  • The variable of the loop will start from a zero index.

  • Print characters which are present at every index.

  • Separate the individual characters.

  • Separate each character by using space.

  • Terminate.

Example Code 1

import java.io.*;

public class divideofaclassrdd {
   public static void main(String[] args){
      String string = "Maitree Express Is An International Railway Connectivity, Runs Between Kolkata To Dhaka And Vice-versa";
      System.out.println("Individual characters from given string is here. Have a look: ");
      for (int i = 0; i < string.length(); i++) {
         System.out.print(string.charAt(i) + " ");
      }
   }
}

Output

Individual characters from given string is here. Have a look: 
M a i t r e e   E x p r e s s   I s   A n   I n t e r n a t i o n a l   R a i l w a y   C o n n e c t i v i t y ,   R u n s   B e t w e e n   K o l k a t a   T o   D h a k a   A n d   V i c e - v e r s a 

Example Code 2

public class SplitaStringtp {
   public static void main(String[] args) {
      String str = "Entire Bengal Province";
      String[] arr = str.split("(?!^)");
      for(String character : arr)
      System.out.print(character + " ");
   }
}

Output

E n t i r e   B e n g a l   P r o v i n c e

Here for this process, the time complexity is O(n), where n is the defined length of that particular string.

Separate the individual characters from a string by using toCharArray method and a for-each loop

To separate the individual characters from a string by using a toCharArray method, we need to follow some step by step process

  • Start.

  • Define a string to start the process.

  • Use toCharArray method.

  • Store the array characters as a return value in a character array

  • Print characters by using a for-each loop.

  • Use space to signify the result.

  • Terminate the whole process.

Example Code 3

import java.io.*;

public class individualdatatprdd{
   public static void main(String[] args){
      String str = "Train Number 13110, Up Maitree Express Runs Between KOAA and DHAKA. It Is An International Railway Connectivity Introduced On 14th April 2008, Monday";
      char[] arr = str.toCharArray();
      System.out.println("Displaying individual characters"+ "from given string. Please have a look and learn how it works:");
      for (char e : arr)
         System.out.print(e + " ");
   }
}

Output

Displaying individual charactersfrom given string. Please have a look and learn how it works:
T r a i n   N u m b e r   1 3 1 1 0 ,   U p   M a i t r e e   E x p r e s s
  R u n s   B e t w e e n   K O A A   a n d   D H A K A .  I t   I s   A n  
  I n t e r n a t i o n a l    R a i l w a y   C o n n e c t i v i t y
  I n t r o d u c e d   O n   1 4 t h   A p r i l   2 0 0 8 ,   M o n d a y 

Conclusion

From this discussion we have found the process to separate the individual characters from a string with some possible java codes by following the syntaxes and algorithm. Hope this article helped you to understand the modus operandi of the cloning processes mentioned here.

Updated on: 12-Apr-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements