-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 
}
Jason C
  • 38,729
  • 14
  • 126
  • 182
user1659644
  • 2,573
  • 3
  • 13
  • 11
  • 5
    _You_ were asked that interview question. – Sotirios Delimanolis Mar 02 '14 at 21:19
  • Way to remove _interview question_. The problem still remains. We aren't here to do your homework. Attempt something. If there is a problem, you come back, giving us all the relevant information, and then we can help you. – Sotirios Delimanolis Mar 02 '14 at 21:28
  • 2
    @JasonC not a duplicate as here indexof isnt allowed – user1659644 Mar 02 '14 at 21:28
  • Why do things the hard way? Why?!? – donfuxx Mar 02 '14 at 21:29
  • @MattBall yes you can use for loops – user1659644 Mar 02 '14 at 21:29
  • 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. – Jason C Mar 02 '14 at 21:30
  • @user1659644 sorry, I wasn't asking if `for` loops were allowed. I was asking why you didn't show any code of your own. – Matt Ball Mar 02 '14 at 21:32
  • @SotiriosDelimanolis struggling to solve this problem, appreciate any help to find solution to this – user1659644 Mar 02 '14 at 21:36
  • 1
    I don't see you struggling, I see you fishing for answers for problems you could work out on your own. – Sotirios Delimanolis Mar 02 '14 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*. – veysiertekin Mar 02 '14 at 21:41
  • @veysiertekin There are some good algorithms listed at http://stackoverflow.com/questions/1765579/fast-algorithm-for-searching-for-substrings-in-a-string?rq=1 too. – Jason C Mar 02 '14 at 22:07
  • You could at least *try* to find the information before asking a question. A Google search of "string search algorithm" gives you plenty of info. – Jim Mischel Mar 03 '14 at 03:43

1 Answers1

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;
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
libik
  • 22,239
  • 9
  • 44
  • 87
  • 4
    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. – Jason C Mar 02 '14 at 21:31
  • 1
    @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. – libik Mar 02 '14 at 21:34
  • 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. – Jason C Mar 02 '14 at 21:36
  • 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. – libik Mar 02 '14 at 21:38
  • 1
    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. – Jason C Mar 02 '14 at 21:40
  • @libik Matt Ball ("Thanks you so much")*n – user1659644 Mar 02 '14 at 21:44
  • @JasonC - good idea with retitling. And if you want to know why I help even these lazy users - I do not consider myself a judge and I dont want be one. There are many others who will judge him in his lifetime. – libik Mar 02 '14 at 21:46
  • @user1659644 = well, you can mark me as accepted to show your thanks :) – libik Mar 02 '14 at 21:49