How to split strings and separate the words with spaces

Split Your String !

Given a string with no spaces in it, how would you split the strings and insert spaces? That’s one of the simple looking yet very vague requirement and is sometimes asked in the interview.

You need to either make assumptions or seek clarifications on many things.

  1. Can the String to split be null? Can it be blank?
  2. Where are the words to compare to located? Is there a dictionary, is there a list of words? Do the words reside in a file?
  3. Can the unslpit string contain more than two words?
  4. And many others.

In the following program that I have written, I have assumed that the string to split can be null, the words to lookup are located in a HashMap and the unsplit word contains only two words (You will see that when I am passing a three word string, the program cannot split the string). Another assumption is that all the words in the HashMap are uppercase and the splitting is case insensitive.


import java.util.HashMap;
import java.util.Map;

 * @author Kushal Paudyal 
 * Created On - Feb 15, 2014 
 * Last Modified On - Feb 15, 2014
public class StringSplitter {

	private static final Map<String, String> DICTIONARY = new HashMap<String, String>();

	public static void main(String args[]) {

		String stringToSplit = "interviewquestions";

		splitString(DICTIONARY, stringToSplit);
		stringToSplit = "googleinterviewquestions";
		splitString(DICTIONARY, stringToSplit);
		//Let's try on more with null string.
		splitString(DICTIONARY, null);


	private static void splitString(Map<String, String> LOOKUP_DICTIONARY, String stringToSplit) {

		boolean splitSuccessful = false;

		if (stringToSplit != null && stringToSplit.length() > 0) {
			for (int index = 0; index < stringToSplit.length(); ++index) {
				String leftSubstring = stringToSplit.substring(0, index);
				String rightSubString = stringToSplit.substring(index);

				System.out.println("LEFT: " + leftSubstring + " RIGHT: " + rightSubString);

				if (DICTIONARY.containsValue(leftSubstring.toUpperCase()) 
						&& DICTIONARY.containsValue(rightSubString.toUpperCase())) {
					System.out.println("MATCH FOUND. SPLIT SUBSTRING: " 
						+ leftSubstring + " " + rightSubString + "\n\n");
					splitSuccessful = true;



		if (!splitSuccessful) {
			System.out.println("Could not split the word! Not found in dictionary\n\n");



Here is the output of running above piece of code.

LEFT:  RIGHT: interviewquestions
LEFT: i RIGHT: nterviewquestions
LEFT: in RIGHT: terviewquestions
LEFT: int RIGHT: erviewquestions
LEFT: inte RIGHT: rviewquestions
LEFT: inter RIGHT: viewquestions
LEFT: interv RIGHT: iewquestions
LEFT: intervi RIGHT: ewquestions
LEFT: intervie RIGHT: wquestions
LEFT: interview RIGHT: questions
MATCH FOUND. SPLIT SUBSTRING: interview questions

LEFT:  RIGHT: googleinterviewquestions
LEFT: g RIGHT: oogleinterviewquestions
LEFT: go RIGHT: ogleinterviewquestions
LEFT: goo RIGHT: gleinterviewquestions
LEFT: goog RIGHT: leinterviewquestions
LEFT: googl RIGHT: einterviewquestions
LEFT: google RIGHT: interviewquestions
LEFT: googlei RIGHT: nterviewquestions
LEFT: googlein RIGHT: terviewquestions
LEFT: googleint RIGHT: erviewquestions
LEFT: googleinte RIGHT: rviewquestions
LEFT: googleinter RIGHT: viewquestions
LEFT: googleinterv RIGHT: iewquestions
LEFT: googleintervi RIGHT: ewquestions
LEFT: googleintervie RIGHT: wquestions
LEFT: googleinterview RIGHT: questions
LEFT: googleinterviewq RIGHT: uestions
LEFT: googleinterviewqu RIGHT: estions
LEFT: googleinterviewque RIGHT: stions
LEFT: googleinterviewques RIGHT: tions
LEFT: googleinterviewquest RIGHT: ions
LEFT: googleinterviewquesti RIGHT: ons
LEFT: googleinterviewquestio RIGHT: ns
LEFT: googleinterviewquestion RIGHT: s
Could not split the word! Not found in dictionary

Could not split the word! Not found in dictionary

[My Video Promotion]
  1. Implementing a Simple LIFO Stack in Java using LinkedList
  2. Implementing a Simple FIFO Queue in Java Using Linked List
  3. Protected: Behavioral Questions – Interview
  4. In Place Matrix (2D Array) Clockwise and Counterclockwise Rotation – Java Implementation
  5. Matrix (2D Array) Clockwise and Counterclockwise Rotation with Extra Buffer – Java Implementation
  6. Array Max Integer Finder (With Big O Analysis)
  7. A Basic Implementation of Binary Tree in Java
  8. A Basic Implementation of Generic Tree in Java
  9. Basic example of implementing Singly Linked List in Java
  10. Top XML Interview Questions
  11. J2EE Interview Questions
  12. Webservice Interview Questions
  13. Core Java Interview Preparation Questions
  14. How to Add and Remove nodes in an unsorted Linked List
  15. Rotating a two dimensional integer array In-Place and using extra memory
  16. How to reverse a Singly Linked List iteratively and recursively
  17. Sorted Circular Linked List Implementation And Insert Operation
  18. Finding Mean Value Of An Integer Array In Java
  19. How to intersect two sets in Java using java.util.Set data structure.
  20. Bubble Sorting An Integer Array In Ascending and Descending Order
  21. How to split strings and separate the words with spaces
  22. Finding if two Strings are Anagrams
  23. How to easily sort characters in a String?
Tagged , , , . Bookmark the permalink.

One Response to How to easily sort characters in a String?

  1. Why visitors still use to read news papers when in this technological world all is available on net?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.