Finding index of needle in haystack recursively.

Brandon Turpy

I am pretty close to completing my function. I need to take 2 strings, and return the index of string 2 inside of string 1. I know there is a find function for this, but I am not able to use it. It also has to be done with recursive programming.

I have the following.

int index_of(string haystack, string needle) {

    int index = 0;
    string test = haystack.substr(index, needle.length());

    if (test == needle) {
        return index;
    }
    else {
        return 1 + index_of(haystack.substr(1), needle);
    }

}

It returns the index of the needle in the haystack no problem but there are 2 things it needs to do I can not figure out.

1) If the needle is not in the haystack, then it needs to return a -1. I have done it so at the end if it does not exist, it returns a -1, but because it is recursive, it then adds the other times it returned 1. I am not sure how to just return a single value at the end without adding all the other instances on it.

2) I am suppose to use a helper function within it and I am not sure how to do that either.

Thanks for any help!

Zach Beavon-Collin

In general, you want to return the value of a recursive function unadulterated. In your case, this:

return 1 + index_of(some_parameters);

Should be this:

return index_of(some_parameters);

Now, you just need to choose parameters such that you can keep track of the index until you need to return it, or alternatively -1.

One such function might have the constructor:

index_of(string haystack, string needle, int index);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding index of needle in haystack recursively.

From Dev

finding needle in haystack, what is a better solution?

From Dev

Byte Pattern Finding (Needle in Haystack) - Wildcard/Mask?

From Dev

Finding needle in a haystack (error when comparing two objects with .equals)

From Dev

Finding needle in haystack with NT script and store the fact it was found or not

From Dev

If either needle in haystack

From Dev

Needle in a haystack jQuery

From Dev

If either needle in haystack

From Dev

Ordering of needle / haystack in Lisp functions

From Dev

What is the problem with this haystack needle function

From Dev

What is the problem with this haystack needle function

From Dev

Javascript: Replace needle in haystack that is not between two characters

From Dev

PDO Find a needle in a haystack SQL statement

From Dev

C++ - Haystack/Needle string check always returns false

From Dev

MYSQL Finding substring when column contains needle

From Dev

Programmatically create haystack index

From Dev

Programmatically create haystack index

From Dev

How to index CommentsField in Haystack?

From Dev

Recursively finding indices of a string

From Dev

Recursively finding with AWK

From Dev

Finding largest file recursively

From Dev

Finding a substring recursively

From Dev

Finding empty directories recursively

From Dev

Regex for finding a string recursively?

From Dev

Recursively finding indices of a string

From Dev

Haystack index on attribute inherited by model

From Dev

Finding "islands" of territories on a map recursively

From Dev

Finding perimeter of a recursively modified square

From Dev

Finding "islands" of territories on a map recursively

Related Related

HotTag

Archive