-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveSpaceWord.java
More file actions
65 lines (50 loc) · 1.66 KB
/
RemoveSpaceWord.java
File metadata and controls
65 lines (50 loc) · 1.66 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.Scanner;
public class RemoveSpaceWord {
private static String result="";
public static void main(String[] args) {
RemoveSpaceWord removeSpaceWord= new RemoveSpaceWord();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Sentence to reduce spaces");
String sentence= scanner.nextLine();
String resultSpaceRemove= reduceSpace(sentence);
System.out.println("Result Sentence (with extra spaces reduced): "+resultSpaceRemove);
System.out.println("Provide a word to be deleted");
String word= scanner.nextLine();
System.out.println("Provide its position number");
int pos= scanner.nextInt();
String resultWordDelete = wordDelete(word,pos);
System.out.println("Result Sentence (with word deleted): "+resultWordDelete);
}
public static String reduceSpace(String sentence) {
for(int i=0;i<sentence.length();i++) {
if(sentence.charAt(i)==' ' && sentence.charAt(i+1) == ' ') { //Loop for removing Space
//for(int j=i+1;j<sentence.length();j++) {
//if(sentence.charAt(i) == sentence.charAt(j)) {
continue;
}
else {
//i=j-1;
//break;
result += sentence.charAt(i);
}
//}
//}
}
return result;
}
static String wordDelete(String word, int pos) {//Condition for deleting duplicate word
String wordDelete = "";
int counter=0;
String sen= RemoveSpaceWord.result;
for(int i=0;i<result.length();i++) {
if(result.charAt(i) == ' ') {
counter++;
}
if(counter == pos-1) {
continue;
}
wordDelete += result.charAt(i);
}
return wordDelete;
}
}