-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSortWithSpace.java
More file actions
42 lines (30 loc) · 756 Bytes
/
StringSortWithSpace.java
File metadata and controls
42 lines (30 loc) · 756 Bytes
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
class StringSortWithSpace
{
public static String StringSortWithSpace(String str){
char[] ch = str.toCharArray();
System.out.println(String.valueOf(ch));
char[] tempArray = new char[ch.length];
for(int i = 0 ; i < ch.length; i++){
if(ch[i] ==' '){
tempArray[i] =' ';
}
}
int j = tempArray.length - 1;
for(int i = 0 ; i < ch.length ; i++){
if(ch[i] !=' '){
if(tempArray[j] ==' '){
j--;
}
tempArray[j] = ch[i];
j--;
}
}
System.out.println(String.valueOf(tempArray));
return String.valueOf(tempArray);
}
public static void main(String[] args)
{
String str = "This is a String";
System.out.println(StringSortWithSpace(str));
}
}