forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.5
52 lines (48 loc) · 1.06 KB
/
1.5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Write a method to replace all spaces in a string with '%20'
*/
import java.util.Scanner;
public class ci_one_five {
public static void main(String argsp[]){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
char ch[] = s.toCharArray();
replaceSpace(ch,s.length());
// String x = "";
// int k = 0;
// for(int i=0;i<s.length();i++){
// char ch = s.charAt(i);
// if(ch==' ')
// x+="%20";
// else
// x+=ch;
//}
//System.out.println(x);
}
public static void replaceSpace(char[] str,int length){
int spaceCount=0,newLen,i=0;
for(i=0;i<length;i++){
if(str[i]==' ')spaceCount++;
}
newLen = length + spaceCount*2;
// str[newLen] = '\0';
System.out.println(str);
char[] new_str = str;
str = new char[newLen];
System.out.println(str);
for(i=length-1;i>=0;i--){
if(new_str[i]==' '){
str[newLen-1] = '0';
str[newLen-2] = '2';
str[newLen-3] = '%';
newLen-=3;
}
else{
str[newLen-1] = new_str[i];
newLen -=1;
}
}
String m = new String(str);
System.out.println(m);
}
}