Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: First string is a substring of another

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default First string is a substring of another

    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?

    All I have is
    public static int substring(String Str1, String Str2)
     {
       index1 = Str2.indexOf(Str1) 
     
     if (index1 >= 0)
     {
      return 1;
     else
     
     }
    Last edited by helloworld922; October 19th, 2009 at 06:52 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: First string is a substring of another

    Here's a simple algorithm to check if a substring is contained within another string:

    1. start from the beginning of the larger string
    2. If the remaining characters is greater than the length of the second string, do 2a. Otherwise, do 2b
    2a. Use the substring() method of the String class to take a chunk that has the same length as the smaller string
    2b. Use the substring() method to take the rest of the letters in the larger string, then add on the remaining characters needed from the beginning
    3. compare if they are equal using the equals() method. If they are equal, return 1 or -1, depending on whether you used 2a or 2b. Otherwise, repeat 2-3 up to the last character in the larger string
    4. If you still haven't found a substring match, return 0.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: First string is a substring of another

    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;
     }
    }
    }

    this is the code that I came up with for this problem can anyone see any errors with it
    if so please help me out in pinpointing them out
    Last edited by helloworld922; October 19th, 2009 at 09:33 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: First string is a substring of another

    You're return statements are pre-mature. The program hasn't completely checked that the string is contained str2 is a substring of str1, but you're saying it is. Also, return is a Java keyword. You can't declare variables called return. There are a few other Java syntax errors (missing semi-colons).

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: First string is a substring of another

    I know I was noticing that about the return statements also

    what would be your suggestions to make sure that the program completely checks that str2 is a substring of str1

  6. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: First string is a substring of another

    is this thread already solved?

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: First string is a substring of another

    public static int substring(String str1, String str2)
    {
         for (int i = 0; i+str2.length() < str1.length(); i++)
         {
              if (str2.equals(str1.substring(i,i+str2.length()))
              {
                   return 1;
              }
         }
         for (int i = str1.length()-str2.length(); i< str1.length(; i++)
         {
              if (str2.equals(str1.substring(i,str1.length)+str1.substring(0,str2.length-i)))
              {
                   return -1;
              }
         }
         return 0;
    }

    Give this a go.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    mgutierrez19 (October 20th, 2009)

  9. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: First string is a substring of another

    tnx hellowolrd... im trying to solve this one.. but im having hard time thinking of how.... hehe

  10. The Following User Says Thank You to chronoz13 For This Useful Post:

    mgutierrez19 (October 20th, 2009)

  11. #9
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: First string is a substring of another

    Thank you guys very much for all you help

  12. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: First string is a substring of another

     for (int i = str1.length() - str2.length(); i < str1.length(); i++) {    
     
     if (str2.equals(str1.substring(i, str1.length() + str1.substring(0, str2.length() - i))))      //cannot find symbol  
       {           
        System.out.println("No Its Not!");        
      }


    Symbol: method subtring(int, java.lang.String)

  13. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: First string is a substring of another

    you're missing a closing parenthesis

    str2.equals(str1.substring(i, str1.length()[b])[/b] + str1.substring(0, str2.length() - i)))

  14. #12
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: First string is a substring of another

    ahh hehehe

  15. #13
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Substring

    - 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” (see example).

    Example:
    - 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

    I had already posted this method before but the last part where it is suppose to check for the circular substring does not seem to be running correctly, can someone look over it and see where i have to make correction on it im not sure if its even right

    public static int subVirus(String Str1, String Str2)
    	{
    		int result = 0;
     		for(int i = 0; i < Str2.length(); i++)
     		{
       			if (Str2.charAt(i) == Str1.charAt(0))
               			for(int j = (i + 1); j < Str2.length(); j++) 
                  			{
               				if (Str2.charAt(j) == Str1.charAt(j))
                 					result = 1;
       				}
     		}
     	                                   for(int i = 0; i < Str2.length(); i++)
                                               {
                                                    if (i = Str2.length())
                                                         i = 0
                                                        for (int j = 0; j < Str2.length(); j++)
     
                                                                 return = -1
                         }
     
    		return result;
    	}

  16. #14
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: First string is a substring of another

    subVirus? I merged your two topics and marked the original one as un-solved.

    Oops, made a few logical errors in my solution a few posts back.

    	public static int substring(String str1, String str2)
    	{
    		if (str2.length() > str1.length())
    		{
    			return 0;
    		}
    		for (int i = 0; i + str2.length() <= str1.length(); i++)
    		{
    			if (str2.equals(str1.substring(i, i + str2.length())))
    			{
    				return 1;
    			}
    		}
    		for (int i = str1.length() - str2.length() + 1; i < str1.length(); i++)
    		{
    			if (str2.equals(str1.substring(i, str1.length()) + str1.substring(
    					0, i - str2.length() + str1.length())))
    			{
    				return -1;
    			}
    		}
    		return 0;
    	}

  17. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 22nd, 2009), mgutierrez19 (October 21st, 2009)

Similar Threads

  1. Substring program, not working
    By Newoor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2009, 12:46 PM
  2. Replies: 5
    Last Post: January 30th, 2009, 09:31 PM
  3. String substring, concatenation operation on linked list
    By oaks12 in forum Collections and Generics
    Replies: 1
    Last Post: January 15th, 2009, 07:12 AM