0

I am trying to write a function(findString) to find out if character strings are present inside another string. For my function, the first argument is the searched character string and the second is the one trying to be found.

If the string is found, then the location of the source string is returned.

My written code is below:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

int findstring(char source[], char lookup[]){
    int lensource, lenlookup;
    lensource= strlen(source);
    lenlookup = strlen(lookup);
    int i;int j;

    for(i=0; i>=lensource; ++i)
        for (j=0; j>=lenlookup; ++j)
            if (source[i]==lookup[j])
                return i;

}

int main(){

    findstring("Heyman","ey");
}

If the function worked properly, then the index 2 should be returned.

However, when I run it, nothing is returned. I suppose the problem it that there is something wrong with my approach with the for loop or if statement.

I'm doing this without using strstr

6
  • my bad I should include that I am trying to avoid that way! Commented Dec 21, 2013 at 8:58
  • How would you know if anything is returned? You don't check the return code of findstring()... Commented Dec 21, 2013 at 9:01
  • 1
    I suggest you have a look at the kmp algorithm also that has a linear time complexity rather than quadratic. en.wikipedia.org/wiki/… Commented Dec 21, 2013 at 9:03
  • Also, the returned index for correct operation is 1. (Assuming 0-indexing) Commented Dec 21, 2013 at 9:06
  • 1
    @BasileStarynkevitch; He can't upvote :) Commented Dec 21, 2013 at 9:11

3 Answers 3

2

First, there is a function that does this already, called strstr.

Second, your loop is written incorrectly. It should be:

for(i=0; i < lensource - lenlookup + 1; ++i) {
    for (j=0; j<lenlookup; ++j)
        if (source[i + j]!=lookup[j])
            break;
    if (j == lenlookup) return i + 1; // edit: had error
                                      // note that return i feels more right, but
                                      // op's spec seems to want i + 1
}
return -1; // no spec on return value for failure, but -1 seems reasonable

Edit: had typo.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks andrey! I was actually doing this without strstr, but I appreciate the help in finding my error.
-1. Nowhere mention in this answer about return value of function.
What I mean to say is, see the OP's findstring function. Its return type is int. But in main he is not assigning this return value to any variable otherwise it is discarded.
You're right that his example snippet looks funny but it doesn't seem like that is the source of his confusion. At least, I can't imagine...
Andrey; Read this line: However, when I run it, nothing is returned.
1

Assuming both source and lookup are non-empty strings, then both lenlookup and lensource are strictly positive.

And your for loops are never executed, because

for(i=0; i>=lensource; ++i) {
  // do something
}

is understood as (and translated by the compiler to):

i=0;
while(i>=lensource) {
  // do something;
  // at last
  ++i;
}

so you understand the initial test i>=lensource is false (at start i==0 but lensource>0) so the loop is never executed

BTW I strongly suggest to compile your code with all warnings and debugging info (e.g. gcc -Wall -g) and to use a debugger (e.g. gdb) to run it step by step and understanding what is happening.

2 Comments

Thx! Makes a ton of sense now!
+1.Your answer is reasonable. I would suggest you to take a look on OP's statement: However, when I run it, nothing is returned..
0
  1. You missed basic input check (what if either source or lookup is NULL, what if lookup is an empty string, what if lookup is longer than source, etc)
  2. The comparison is not performed correctly
  3. Your function doesn't always return a value (what does it return if lookup is not found in source?).
int findstring(char source[], char lookup[]){

    // when either input is NULL
    if(source==NULL||lookup==NULL)return -1;

    int lensource, lenlookup;
    lensource= strlen(source);
    lenlookup = strlen(lookup);

    // when lookup is an empty string
    if(lenlookup==0)return 0;

    // when lookup is longer than source
    if(lenlookup>lensource)return -1;

    int i;int j;

    for(i=0; i<=lensource-lenlookup; ++i){
        bool success = true;
        for (j=0; j<=lenlookup; ++j){
            if(lenlookup[j]!=lenlookup[i+j]){
                success = false;
                break;
            }
        }
        if(success)return i;
    }

    return -1;
}

You may also want to check out other more efficient algorithms like KMP, boyer-moore and rabin-karp.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.