-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveSpaces.java
More file actions
47 lines (42 loc) · 1.33 KB
/
RemoveSpaces.java
File metadata and controls
47 lines (42 loc) · 1.33 KB
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
package com.company;
/**
* fourthLab
*/
public class RemoveSpaces {
public static void removeSpacesRecursively(char [] arr, int i){
if(i == arr.length || i + 1 == arr.length){
return;
}
if(arr[i] == ' ' && arr[i+1] == ' ') {
for (int j = i; j < arr.length - 1; j++) {
arr[j] = arr[j + 1];
}
arr[arr.length - 1] = '\0';
removeSpacesRecursively(arr, i);
}
else{
removeSpacesRecursively(arr, i + 1);
}
}
/*(symbol <= 'z' && symbol >= 'a') || (symbol >= 'A' && symbol <= 'Z')*/
public static char[] removeSpacesIteratively(String str){
char[] arr = str.toCharArray();
for (int i = 0; i < arr.length - 1; i++) {
while (arr[i] == ' ' && arr[i+1] == ' '){
for (int j = i; j < arr.length - 1; j++) {
arr[j] = arr[j + 1];
}
arr[arr.length - 1] = '\0';
}
}
return arr;
}
public static void main(String[] args) {
String str = "This is a test for test";
char [] arr = removeSpacesIteratively(str);
System.out.println(arr);
char [] arr2 = str.toCharArray();
removeSpacesRecursively(arr2, 0);
System.out.println(arr2);
}
}