Results 1 to 7 of 7
  1. #1
    evant8950 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default String Comparison

    Hey guys,

    I need some help, I need to write an algorithm to compare two strings and return -1 is the first string comes before the second one or 1 if the second comes before the first or 0 if they are equal. I can't figure out what to do this with recursion. Any help will be appreciated

    Thanks

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you can use chartAt to compare(>, <) each character in the String

  3. #3
    evant8950 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    Thats what I am thinking, but I can't determine what the base case would be.

    Thanks

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    erm... you've looked at at the source code for String.compareTo(), right...?

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    SampleCode
    -------------
    class Sample
    {
    public static void main(String args[])
    {
    String first = "ramya";
    String second = "sivakanth";
    String third ="ramya";
    System.out.println(first.compareTo(second));
    System.out.println(second.compareTo(first));
    System.out.println(first.compareTo(third));
    }
    }

Similar Threads

  1. list comparison
    By suprabha in forum New To Java
    Replies: 0
    Last Post: 07-17-2008, 11:01 AM
  2. Object Comparison!
    By sajdutt in forum Advanced Java
    Replies: 6
    Last Post: 05-23-2008, 01:48 PM
  3. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 04:47 AM
  4. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 02:46 AM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 12:16 AM

Posting Permissions

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