Last active
May 29, 2018 02:34
-
-
Save thmain/4b78f7c94ee8b7e79c25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReplaceAllSpaces { | |
public void replace(String s1, int length) { | |
char[] chars = s1.toCharArray(); | |
int spaceCount = 0; | |
for (int i = 0; i < length; i++) { | |
if (chars[i] == ' ') { | |
spaceCount++; | |
} | |
} | |
int newLength = length + 2 * spaceCount; | |
char [] charsNew = new char [newLength]; | |
for (int i = length - 1; i >= 0; i--) { | |
if (chars[i] == ' ') { | |
charsNew[newLength - 1] = '0'; | |
charsNew[newLength - 2] = '2'; | |
charsNew[newLength - 3] = '%'; | |
newLength = newLength - 3; | |
} else { | |
// System.out.println(chars[i]); | |
charsNew[newLength - 1] = chars[i]; | |
newLength = newLength - 1; | |
} | |
} | |
System.out.println("Output String : " + String.valueOf(charsNew)); | |
} | |
public static void main(String args[]) { | |
String s1 = "I am Sumit Jain"; | |
int trueLength = s1.length(); | |
System.out.println("Input String : " + s1); | |
ReplaceAllSpaces r = new ReplaceAllSpaces(); | |
r.replace(s1, trueLength); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment