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);
     }

}

1 comment:

  1. I'm glad I found this web site, I couldn't find any knowledge on this matter prior to.Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, im always look for people to check out my web site.
    cs代写

    ReplyDelete