About find one string in another string
Write a method called findInString that takes two Strings as parameters, and returns the
index of the start of the first occurrence of the second parameter in the first parameter, or
-1 if the second parameter does not occur in the first parameter.
For example, if we call findInString with the first parameter as 揟his is a test? and the
second parameter as 搃s? the method should return 2, since the first occurrence of 搃s?br>starts at index 2. If the second parameter was 揈s? the method should return -1 since the
String 揈s?does not occur in the String 揟his is a test?
I know that it is very easy to solve this problem by using the indexOf method, but the thing is that indexOf is not allowed . Only the following methods are allowed : charAt, compareTo,
equals, equalsIgnoreCase, length, replace, substring, toLowerCase, and
toUpperCase.
Please give me some help.
[926 byte] By [
Ace1a] at [2007-11-27 6:16:20]

What have you done so far? What is the problem?
kajbja at 2007-7-12 17:27:54 >

Hi,
this is my code. The problem is that when I run the program, no matter what two strings I input, it always return -1. Please help me.
public static int findInString (String input1, String input2)
{
int answer = 99999;
int initial = 0;
for (initial = 0; initial < input1.length(); initial ++)
{
if ( ( initial + input2.length() ) > input1.length())
{ answer = -1; break; }
else
{
if (input1.charAt (initial) == input2.charAt (0) && input2 == input1.substring ( initial, initial + input2.length()))
{ answer = initial; break; }
else
{ answer = -1; }
}
}
return answer;
}
Ace1a at 2007-7-12 17:27:54 >

Are you required to code this yourself. If not there is a String method you can use.
> Are you required to code this yourself. If not there
> is a String method you can use.
The indexOf that is specifically not allowed?
Ace. Your code is kind of painful to look at. As a tip when you end up doing things like
int answer = 99999;
You are doing something not good. That isn't a scalable solution.
I just wrote my own version of your homework (this is case sensitive but could easily be changed). However I am not going to give you the full solution.. but I will post most of it so that you can get an idea of how using more than one method makes the code simpler.
public class Test{
public static void main(String args[]){
System.out.println(findInString("This is a test","is"));
System.out.println(findInString("alphabet soup","is"));
System.out.println(findInString("This is a test","This is a test This is a test"));
}
public static int findInString(String toSearch, String toMatch){
for(int i=0;i<toSearch.length();i++){
if(toSearch.length()-i >< toMatch.length()){
// what should we return here. and why?
return ?;
}
if(matchesAtPos(toSearch,toMatch,i)){
// what should we return here and why?
return ?;
}
}
return -1;
}
private static boolean matchesAtPos(String toSearch,String toMatch,int pos){
for(int i=0;i<toMatch.length();i++){
// CODE HERE
if ( something ) {
return ?;
}
}
return ?;
}
}
>
> > Are you required to code this yourself. If not> there> > is a String method you can use.> > The indexOf that is specifically not allowed? My bad, it helps if you read the entire post.
For ***** SAKE THIS STUPID ******* FORUM!!!!!!
This really, really, really sucks goatballs. Cripes. It ***** up < and when I replace it with < it ***** that up too.
Christ Sun! This is how to fix this stupid bug.
Regex replace every < with & lt ;
Regex replace every > with & gt ;
Then you don't have to be so paranoid about the god-damned HTML tags and **** up all the code posts.
Ace,
The line of code that looks like this
if(toSearch.length()-i >< toMatch.length()){
God knows it will probably turn out fine here...
Anyway that should be
if(toSearch.length()-i (SYMBOL FOR LESS THAN HERE) toMatch.length()){
In all my code postings I have never been bitten by the >< bug. Obviously I don't taste very good.
> In all my code postings I have never been bitten by
> the >< bug. Obviously I don't taste very good.
I thought it was cute when it buggered up & lt ; as well. I escaped the & so it was & ; lt ;
Just utter ****. i have no idea why it does it either.