File tree Expand file tree Collapse file tree 1 file changed +4
-1
lines changed 
src/me/ramswaroop/strings Expand file tree Collapse file tree 1 file changed +4
-1
lines changed Original file line number Diff line number Diff 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    }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments