Skip to content

Commit 80abc18

Browse files
author
Ram swaroop
committed
added comments
1 parent 5153324 commit 80abc18

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/me/ramswaroop/strings/RemoveExtraSpaces.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ public class RemoveExtraSpaces {
1919
* @return
2020
*/
2121
public static String removeExtraSpaces(String s) {
22+
2223
char[] c = s.toCharArray();
23-
2424
int j = c.length;
25+
2526
for (int i = 1; i < c.length; i++) {
2627
// check for two or more consecutive spaces
2728
if (c[i] == ' ' && c[i - 1] == ' ') {
@@ -39,6 +40,7 @@ public static String removeExtraSpaces(String s) {
3940

4041
// copy characters occurring after extra spaces to their appropriate positions
4142
while (i < c.length && j < c.length) {
43+
// stop when you encounter extra spaces again
4244
if (c[i] == ' ' && c[i - 1] == ' ') break;
4345

4446
c[j] = c[i];
@@ -53,6 +55,7 @@ public static String removeExtraSpaces(String s) {
5355
public static void main(String a[]) {
5456
System.out.println(removeExtraSpaces("ram swaroop is a good boy."));
5557
System.out.println(removeExtraSpaces("ram swaroop is a good boy."));
58+
System.out.println(removeExtraSpaces(" ram swaroop is a good boy."));
5659
System.out.println(removeExtraSpaces("ram swaroop is a good boy ."));
5760
System.out.println(removeExtraSpaces(" ram swaroop is a good boy ."));
5861
}

0 commit comments

Comments
 (0)