Results 1 to 4 of 4

Thread: using arrays

  1. #1
    monikat is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default using arrays

    hi,

    i want to write a program in java where it will take strings as input and it will compare the strings with others and print the longest prefix common among them...

    plz help..

    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So what have you got so far? Please use code tags when posting code (see my signature for details on how to do this). Best of luck!

  3. #3
    monikat is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    i dont know how to start just i know the logic that by using array we can store strings and then by using for loop we will compare each letter of first string with second and so on. plz tell me the code..
    Last edited by monikat; 04-17-2010 at 11:19 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by monikat View Post
    i dont know how to start just i know the logic that by using array we can store strings and then by using for loop we will compare each letter of first string with second and so on. plz tell me the code..
    Analyze this little problem to death first and only then go from the logic to the Java implementations; I start: you have a set (or array) of Strings: pS1, pS2, ... pSn where p is the longest common prefix; the task is to find p.

    For the first two Strings pS1 and pS2 you don't know any prefix yet so it is safe to assume that the lenth of the prefix is at most |pS1| == n. The job reduces to finding a common longest prefix of two strings Si and Sj of at most n characters. The common prefix has at most n characters. Also, n can not be larger than |Si| or |Sj|.

    Here is where Java kicks in:

    Java Code:
    public int prefixLength(String si, String sj, int n) {
       n= Math.min(Math.min(n, si.length()), sj.length()); // find a posible valid n
       
       // find the length of the largest prefix of si and sj
       for (int i= 0; i <= n; i++)
          if (!sj.startsWith(si.substring(0, i))
             return i-1; // that last char didn't match so i-1
       return n; // all n characters matched
    }
    The method above finds the length of the longest common prefix of two Strings s1 and s2; the prefix has at most n characters. This little method is the heart of your program; I did the hard part; all you have to do is apply this method for all your Strings in an array. Oh, and you have to read those Strings from somewhere first.

    kind regards,

    Jos

Similar Threads

  1. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  2. Arrays
    By karabo101 in forum New To Java
    Replies: 12
    Last Post: 10-11-2009, 05:02 PM
  3. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 03:15 AM
  4. 2D Arrays
    By Major90 in forum New To Java
    Replies: 6
    Last Post: 11-06-2008, 02:08 PM
  5. Need help with Arrays
    By dietgal in forum New To Java
    Replies: 21
    Last Post: 10-08-2008, 01:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •