WAP in java to take a sentence from the user and remove the extra spaces
Example :-
Output :My name is Khan
Input : My name is Khan
Answers
Answered by
1
Answer:
As the program stated we need to remove all the spaces.
The spaces is called here the white space. Now to remove those white spaces in JAVA we have a string function named 'trim' which will remove all the extra white spaces from a string and return the moderate one.
Here we have use another string function 'replaceAll' which will return any string after having its particular operation.
class removeSpace {
public static void main(String args[]) {
String s = "My name is Khan ";
System.out.println(s.replaceAll("\\s+"," ").trim());
}
}
Similar questions