Arranging words within a given string alphabetically
<p>i'm just trying to write a code for arranging the words within  a string alphabetically..
But whenever i am running this it' entering in an infinite loop.. i cant figure out what is exactly happening..can  anybody help me out plzzz.. below i've attached my code.</p>

<pre><code>public class AscendString {
    String s=new String();

    public AscendString(String x)
    {
        s=x.trim();
    }

    public int NoWords()
    {
        int i=0;
        String s1=new String();
        s1=s;
        while(s1.length() &gt; 0)
        {   i++;
            int j=s1.indexOf(' ');
            if(j&gt;0)
            {
                s1.substring(j+1);
                s1=s1.trim();
            }
            else
            s1="";
        }
        return i;
    }

    public void Ascend()
    {
        String str[]=new String[NoWords()];
        String s1=new String();
        s1=s;
        int i=0;
        while(s1.length() &gt; 0)
        {
            int j=s1.indexOf(' ');
            if(j&gt;0)
            {
                str[i]=s1.substring(0,j) ;
                s1=s1.substring(j+1);
                s1=s1.trim();
                i++;
            }
            else
            {
                str[i]=s1;
                s1="";
            }
        }
        for(int j=0;j &lt; str.length-1;j++)
        {
            for(int k=0;k &lt; str.length-1-j;k++)
            if(str[k].length() &gt; str[k+1].length())
            {String temp=str[k];
                str[k]=str[k+1];
                str[k+1]=temp;
            }
        }
        String str1="";
        for(int n=0;n &lt; str.length;n++)
        str1=str1+str[n] +" " ;
        System.out.println("The String in Alphabetic Order is: "+str1);
    }

    public static void main(String args[])
    {
        AscendString exmpl=new AscendString("I Love Java Programming");
        exmpl.Ascend();
    }
}
</code></pre>
