Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 29, 2018 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/4b78f7c94ee8b7e79c25 to your computer and use it in GitHub Desktop.
Save thmain/4b78f7c94ee8b7e79c25 to your computer and use it in GitHub Desktop.
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