-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURLify.java
45 lines (37 loc) · 977 Bytes
/
URLify.java
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
package data.structure;
public class URLify {
public static void main(String args []) {
java.util.Scanner sc = new java.util.Scanner(System.in);
char charArray[] = sc.nextLine().toCharArray();
System.out.println(charArray.length);
int j = charArray.length-1;
int trueLength = charArray.length;
while(charArray[j] == ' ') {
charArray[j--] ='\0';
trueLength--;
}
System.out.println(trueLength);
int spaceCount =0;
for( int i = 0 ; i<charArray.length;i++)
{
if(charArray[i] == ' ')
spaceCount++;
}
int index = trueLength + 2*spaceCount;
char charArray2[] = new char[index];
for( int i = trueLength-1 ; i>=0 ; i--)
{
if(charArray[i] == ' ')
{
charArray2[--index] = '0';
charArray2[--index] = '2';
charArray2[--index] = '%';
}
else
charArray2[--index] = charArray[i];
}
for(char c : charArray2)
System.out.print(c);
sc.close();
}
}