-2

You are not allowed to use any inbuilt functions like indexOf(), contains() or matches() of String class.

Find string apple in string webapple using given char arrays?

String webapple ="webapple";
String apple="apple";
char[] webappleArray=webapple.toCharArray();
char[] appleArray = apple.toCharArray();

write a function

public boolean isPresent(char[] apple ,char[] webapple ){
    //your code here 
}
12
  • 5
    You were asked that interview question. Commented Mar 2, 2014 at 21:19
  • 2
    @JasonC not a duplicate as here indexof isnt allowed Commented Mar 2, 2014 at 21:28
  • 1
    @user1659644 Then use your programming skills and figure out the logic, once you have attempted something, when you are having a specific problem, feel free to come back here and ask about it. If you cannot even attempt this on your own, then you may wish to consider interviewing for a different type of job. Commented Mar 2, 2014 at 21:30
  • 1
    I don't see you struggling, I see you fishing for answers for problems you could work out on your own. Commented Mar 2, 2014 at 21:38
  • 1
    Best algorithm to find a text is present or not present in a text, is Horspool Algorithm. I know you may ask for your homework or your interview, check Google "how it works", "how can you write on Java", briefly: make a little effort. Commented Mar 2, 2014 at 21:41

1 Answer 1

4

I add it here in case someone really need it or want to study from it:

public static void main(String[] args) {
    String webapple = "webapple";
    String apple = "apple";
    char[] webappleArray = webapple.toCharArray();
    char[] appleArray = apple.toCharArray();
    System.out.println(isPresent(appleArray, webappleArray));
}

public static boolean isPresent(char[] apple, char[] webapple) {
    for (int i = 0; i < webapple.length - apple.length+1; i++) {
        for (int j = 0; j < apple.length; j++) {
            if (webapple[i + j] == apple[j]) {
                if (j == apple.length - 1) {
                    return true;
                }
            } else {
                break;
            }
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Dude. I don't hope he "burns in hell", but I do hope he writes some software that ends up frustrating you with stability and usability issues some day. Your answer does an extreme disservice to the asker.
@JasonC - You find a lot and lot and lot unanswered question on the internet, which is frustrating a lot more than someone bad who has no chance to actually program anything.
You would find far less questions on the internet if more meaningful and long lasting answers were given to begin with. Answers like yours are precisely why the OP, after failing to learn basic problem solving skills here (at minimum you merely showed him what a for loop does), will end up asking yet another unanswered question in the future.
Actually, if they ask answer that is not answered on stackoverflow (this one is not, at least in popular or threadname-similar question), it will be my pleasure.
My disagreements aside, if you believe that is the case, consider editing the OP's question to give it a title that is more descriptive, so others can find it in the future instead of reasking the same question. If you are going on this quest to improve the community, either go all the way or don't bother.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.