# First string is a substring of another

• October 19th, 2009, 04:25 PM
mgutierrez19
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
Code :

```public static int substring(String Str1, String Str2) { index1 = Str2.indexOf(Str1)   if (index1 >= 0) { return 1; else   }```
• October 19th, 2009, 06:57 PM
helloworld922
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.
• October 19th, 2009, 09:20 PM
mgutierrez19
Re: First string is a substring of another
Code :

```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
• October 19th, 2009, 09:59 PM
helloworld922
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).
• October 19th, 2009, 10:13 PM
mgutierrez19
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
• October 20th, 2009, 05:47 AM
chronoz13
Re: First string is a substring of another
• October 20th, 2009, 09:41 AM
helloworld922
Re: First string is a substring of another
Code :

```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.
• October 20th, 2009, 10:00 AM
chronoz13
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 \m/
• October 20th, 2009, 10:28 AM
mgutierrez19
Re: First string is a substring of another
Thank you guys very much for all you help
• October 21st, 2009, 04:19 AM
chronoz13
Re: First string is a substring of another
Code :

``` 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)
• October 21st, 2009, 09:18 AM
helloworld922
Re: First string is a substring of another
you're missing a closing parenthesis :)

Code :

`str2.equals(str1.substring(i, str1.length()[b])[/b] + str1.substring(0, str2.length() - i)))`
• October 21st, 2009, 09:52 AM
chronoz13
Re: First string is a substring of another
ahh hehehe
• October 21st, 2009, 10:10 PM
mgutierrez19
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

Code :

```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; }```
• October 21st, 2009, 10:35 PM
helloworld922
Re: First string is a substring of another
subVirus? :D 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.

Code :

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