This discussion is archived
8 Replies Latest reply: Oct 19, 2009 2:18 PM by 796440 RSS

first string is a substring of the second one

807580 Newbie
Currently Being Moderated
I need to write a User Defined Method which
Checks if the first string is a substring of the second one.
- Returns 0 if it is not a substring / 1 if it is / -1 if it is a “circular substring”
this is an example of what it should do

-     abcde is a substring of blkjabcdelkjsd  return 1
-     abcde is not a substring of slfkjsdlskggfhjkh  return 0
-     abcde is a “circular substring” of delkjlkjabc  return -1

Can Anyone help?
  • 1. Re: first string is a substring of the second one
    796447 Newbie
    Currently Being Moderated
    I would suggest using a computer with a keyboard, and a code editor product. And pay attention in class instead of just dumping your homework assignments here.
  • 2. Re: first string is a substring of the second one
    807580 Newbie
    Currently Being Moderated
    I have Tried doing it just dont know how to get the circular substring.

    this is what i have so far
    public static int substring(String Str1, String Str2)
     {
       index1 = Str2.indexOf(Str1) 
    
     if (index1 >= 0)
     {
      return 1;
     else
      
     }
  • 3. Re: first string is a substring of the second one
    796440 Guru
    Currently Being Moderated
    1. Ask a concrete question. "It doesn't work," "How do I do it?" etc. don't provide enough information for anybody to be able to help you.

    2. Ask your question only once. You seem to have two threads for the same question. I'm not going to waste my time trying to figure out which is the "main" one.
  • 4. Re: first string is a substring of the second one
    796447 Newbie
    Currently Being Moderated
    mgutierrez19 wrote:
    I have Tried doing it just dont know how to get the circular substring.

    this is what i have so far
    public static int substring(String Str1, String Str2)
    {
    index1 = Str2.indexOf(Str1) 
    
    if (index1 >= 0)
    {
    return 1;
    else
    
    }
    Big hint: Concatenate the string to the end of itself.
    Edit: I am assuming it does not need to go around the circle more than once, so for example:
    "aaa" is not a circular substring of "a" (or you'd have to circle around twice).
  • 5. Re: first string is a substring of the second one
    807580 Newbie
    Currently Being Moderated
    I understand that I have to concatenate the string to itself the only problem is that with i dont know where to add or how to add that in the method im using since i am searching for the substring using indexOf
  • 6. Re: first string is a substring of the second one
    796447 Newbie
    Currently Being Moderated
    mgutierrez19 wrote:
    I understand that I have to concatenate the string to itself the only problem is that with i dont know where to add or how to add that in the method im using since i am searching for the substring using indexOf
    (sigh)
    Use indexOf on the original string like you are currently doing. If that doesn't make a match, then:
    Create a new string by concatenating the original string with itself, and then use indexOf on that new string.
    Come on, engage your brain a bit. I'm not going to completely spell it out for you (I just about have already).
  • 7. Re: first string is a substring of the second one
    807580 Newbie
    Currently Being Moderated
    I need to write a User Defined Method which
    Checks if the first string is a substring of the second one.
    - Returns 0 if it is not a substring / 1 if it is / -1 if it is a “circular substring”
    this is an example of what it should do

    - abcde is a substring of blkjabcdelkjsd  return 1
    - abcde is not a substring of slfkjsdlskggfhjkh  return 0
    - abcde is a “circular substring” of delkjlkjabc  return -1

    Can Anyone help?

    This is the code that i have so far but not sure if it is correct with the return values
    public static int substring(String Str1, String Str2)
     {
     int return = 0 
     for(int i = 0; i < Str2.length(); i++)
     {
       if (Str2.charAt(i) == Str1.charAt(0))
               for(int j =0; j < Str2.length(); j++) 
                  {
               if (Str2.charAt(i) == Str1.charAt(j))
    
                 return = 1;
               else 
                 return = 0;
       }
     }
     for(int i = 0; i < Str2.length(); i++)
     {
       if (i = Str2.length())
           i = 0
         for (int j = 0; j < Str2.length(); j++)
        
         return -1;
     }
    }
    }
  • 8. Re: first string is a substring of the second one
    796440 Guru
    Currently Being Moderated
    mgutierrez19 wrote:
    This is the code that i have so far but not sure if it is correct with the return values
    So write simple test code that provides various inputs and see if your code gives the expected results.