1import java.util.Scanner;
2class RemoveExtraSpace{ //starting of class
3 public static void main(String[] args){ //starting of the main function
4 Scanner sc=new Scanner(System.in); //declaring an object of the Scanner class
5 String input,output="";
6 char c;
7 int i,len;
8 System.out.println("ENTER A SENTENCE.");
9 input=sc.nextLine().trim(); //accepting the input
10 len=input.length();
11 for(i=0;i<len;i++){ //outer for loop to traverse each character
12 c=input.charAt(i);
13 output+=c;
14 if(c==' '){
15 while(input.charAt(i)==' '){ //inner while loop to skip through repeated spaces
16 i++;
17 } //end of while loop
18 i--; //decrementing i by 1 so that the immediate character after space is taken
19 }
20 } //end of for loop
21 System.out.println(output); //printing the result
22 } //closing of main function
23} //closing of class