String s1 = "MADAM"; String s2 = ""; int k = 1; while (k <= s1.length()) { s2 = s2 + s1.substring(0, k); s2 = s2 + " "; k = k + 1;