Question #223346

Write a program that replaces two or more consecutive blanks in a string by a

single

blank. For example, if the input is

Grim return to the planet of apes !!

the output should be

Grim return to the planet of apes!!


Expert's answer



import java.util.Scanner;
public class ReplaceBlanks {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); 
        String text=input.nextLine();     
        System.out.println(new String(text).trim().replaceAll("\\s{2,}", " "));
        input.close();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS