Reverse words in a char array. Can anyone tell me what is wrong with this implementation?
Anonymous User
131
		/*
		eg input:  
		arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', '  ',                 'm', 'a', 'k', 'e', 's', '  ',                 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ]  
		output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', '  ',           'm', 'a', 'k', 'e', 's', '  ',           'p', 'e', 'r', 'f', 'e', 'c', 't' ] 
		*/

		int start=0,end=arr.length-1;

	    arr=swap(arr,start,end);
	 
	    
	    int startIdx=-1,endIdx=-1;
	    for(int i=0;i<arr.length;i++){
	        if(arr[i]==' ' && startIdx!=-1){
	            endIdx=i-1;
	             swap(arr,startIdx,endIdx);
	          
	        }else if(i==arr.length-1 && startIdx!=-1){
	             endIdx=i;
	             swap(arr,startIdx,endIdx);
	          }
	        else if(startIdx==-1){
	            startIdx=i;
	          }   
	      }
	    return arr;
	  }

	  public static void swap(char arr[],int start,int end){
		    while(start<end){
			  char temp=arr[start];
			  arr[start]=arr[end];
			  arr[end]=temp;
			  start++;
			  end--;
		   }
		    
		    
		    
	  }```
Comments (1)