File tree 1 file changed +4
-1
lines changed
src/me/ramswaroop/strings
1 file changed +4
-1
lines changed Original file line number Diff line number Diff line change @@ -19,9 +19,10 @@ public class RemoveExtraSpaces {
19
19
* @return
20
20
*/
21
21
public static String removeExtraSpaces (String s ) {
22
+
22
23
char [] c = s .toCharArray ();
23
-
24
24
int j = c .length ;
25
+
25
26
for (int i = 1 ; i < c .length ; i ++) {
26
27
// check for two or more consecutive spaces
27
28
if (c [i ] == ' ' && c [i - 1 ] == ' ' ) {
@@ -39,6 +40,7 @@ public static String removeExtraSpaces(String s) {
39
40
40
41
// copy characters occurring after extra spaces to their appropriate positions
41
42
while (i < c .length && j < c .length ) {
43
+ // stop when you encounter extra spaces again
42
44
if (c [i ] == ' ' && c [i - 1 ] == ' ' ) break ;
43
45
44
46
c [j ] = c [i ];
@@ -53,6 +55,7 @@ public static String removeExtraSpaces(String s) {
53
55
public static void main (String a []) {
54
56
System .out .println (removeExtraSpaces ("ram swaroop is a good boy." ));
55
57
System .out .println (removeExtraSpaces ("ram swaroop is a good boy." ));
58
+ System .out .println (removeExtraSpaces (" ram swaroop is a good boy." ));
56
59
System .out .println (removeExtraSpaces ("ram swaroop is a good boy ." ));
57
60
System .out .println (removeExtraSpaces (" ram swaroop is a good boy ." ));
58
61
}
You can’t perform that action at this time.
0 commit comments