Wednesday, 8 January 2014

Java: Write a program to compact spaces in a string

Write a program to compact spaces in a string i.e. Given a string you have to remove multiple occurrences of blank spaces by a single space and do that in a single pass of the arrays

public class HelloWorld{

     public static void main(String []args){
         String s1="Hello    kiran   how are   you";
    s1=s1.trim().replaceAll("\\s+", " ");
    System.out.println(""+s1);
     }

}

No comments:

Post a Comment