-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveAllExtraBlankSpaces.java
More file actions
56 lines (51 loc) · 981 Bytes
/
Copy pathRemoveAllExtraBlankSpaces.java
File metadata and controls
56 lines (51 loc) · 981 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//39.Write a JAVA program to remove all extra blank spaces from given string.
import java.util.*;
public class RemoveAllExtraBlankSpaces
{
public static void main(String x[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String ");
String s=sc.nextLine();
int start=0;
int end=0;
for(int i=0; i<s.length(); i++)
{
if(s.charAt(i)!=' ')
{
start=i;
break;
}
}
for(int i=s.length()-1;i>=0; i--)
{
if(s.charAt(i)!=' ')
{
end=i;
break;
}
}
// String str=new String("");
StringBuilder str=new StringBuilder();
for(int i=start; i<=end; i++)
{
if(s.charAt(i)==' ' && s.charAt(i+1)==' ')
{
continue;
}
else
{
str.append(s.charAt(i));
//str=str+s.charAt(i);
}
}
/*
for(int i=0; i<s.length(); i++)
{
str.append(s.charAt(i));
}
*/
System.out.println(str);
System.out.println(str.length());
}
}