Go to Top

Thursday 11 August 2011

Utilities - 2 : How to jumble up words ?

                         Hi folks! Recently I had created a Java Program to jumble up words. And I am here to share it with you all. Here's the program. Just copy and paste and compile this program :

Java Program : 


import java.io.*;
import java.util.*;
class jumbled_updated
{
  static String s,s1="";
  static char[] a;
  static String[] b;
  static void main()throws IOException
  {
      input();
      separate();
      jumble();
      display();
    }
    static void input()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the word/s : ");
        s=br.readLine();
        if(s.length()<3)
        {
            System.out.print("\nPlease enter atleast three characters\n\n");
            input();
        }
        a=new char[s.length()];
    }
    static void separate()
    {
        int i,u=0,l=0,c1=0;
        char k,c=1;
        for(i=0;i<s.length();i++)
        {
            k=s.charAt(i);
            if(k==' ')
            c++;
        }
        b=new String[c];
        for(i=0;i<s.length();i++)
        {
            k=s.charAt(i);
            if(k==' ')
            {
                u=i;
                b[c1]=s.substring(l,u);
                l=u+1;
                c1++;
            }
        }
        b[c1]=s.substring(l,s.length());
    }
    static void jumble()
    {
        int c1,c2,i,j,k,c=0,c3=2;
        char k1;
        Random r1=new Random();
        Random r2=new Random();
        for(i=1;i<=b.length;i++)
        {
            a=new char[b[c].length()];
            for(j=0;j<b[c].length();j++)
            a[j]=b[c].charAt(j);
            for(k=0;k<=s.length()*3;k++)
            {
                c1=r1.nextInt(b[c].length()-1);
                c2=r2.nextInt(b[c].length()-1);
                k1=a[c1];
                a[c1]=a[c2];
                a[c2]=k1;
            }
            for(k=0;k<a.length;k++)
            s1=s1+a[k];
            if(c3<=b.length)
            s1=s1+" ";
            c++;
            c3++;
        }
    }
    static void display()
    {
        System.out.print("\nThe jumbled word is : "+s1);
        s1="";
    }
}

Please comment if you like this post.

1 comment:

  1. //Optimized code

    package pk;

    import java.io.*;
    import java.util.Random;

    public class Annagrams {





    public static void main(String[] args)throws Exception

    {
    String s2="",s1="";
    int len,rand;
    Random r1=new Random();
    System.out.println("Enter the word you want to jumble:");
    BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
    s1=br.readLine();
    StringBuilder s=new StringBuilder(s1);

    len=s1.length();


    do
    {
    rand=r1.nextInt(len);
    s2=s2+s.charAt(rand);
    s.deleteCharAt(rand);
    len--;


    }while(len>0);


    System.out.println("The Jumbled word is :"+s2);
    }

    }

    ReplyDelete

ShareThis