//For example: I’m very happy to introduce m yself --> to you here
// To I’m very happy to introduce m yself --> to you here
//Codes:
String str = " I’m very happy to introduce m yself --> to you here ";
char[] charArray = str.toCharArray();
int index = 0;
int movePoint = 0;
for(; index<charArray.length; index++){
if(charArray[index] == ' '){
while(movePoint<charArray.length && charArray[movePoint] == ' '){
movePoint++;
}
while(movePoint<charArray.length && charArray[movePoint] != ' '){
index++;
charArray[index] = charArray[movePoint];
movePoint++;
}
if(movePoint >=charArray.length ){
break;
}
} else{
if(index != 0){
charArray[index] = ' ';
index = index-1;
}
}
}
String result = String.valueOf(charArray, 0, index+1);
System.out.println(result);