views:

315

answers:

6

Write a time efficient C program that takes two strings (string a, string b) and gives the first occurence of string b in string a.

+2  A: 

Wikipedia knows all

Paul
+3  A: 

Many algorithm is on string matching. For example,Knuth–Morris–Pratt algorithm, Boyer-Moore algorithm. Just refer to any one algorithm handbook.

cnhk
+1  A: 

Write an efficient C program that takes two strings (string a, string b) and gives the first occurence of string b in string a.

It doesnt say that you are doing repeated matching against either string, or any useful insight about one being particularly short, specific content, or there being plenty of startup time then a trigger event after which a fast-as-possible comparison is needed, so: all the sophisticated algorithms mentioned in other answers probably are not sought by the interviewer.

I read "efficient" to mean that the algorithm is not iterating and invoking an out-of-line strcmp(), mindful not to repeatedly call strlen(), preferably returns false immediately if the equality comparison exhausts "haystack" before "needle". Honestly, if it is an early screening interview, then enough people would fail to implement something like that well - it is very believable that that is all they wanted without going into advanced prior indexing or state machines.

Tony
@Tony, efficiency means time efficiency from interviewer perspective.
siva
When I ask interviewees questions like this, I don't provide information about repeated matches etc. I expect the interviewee to realise they need nore information and ask me for it. That's really what I'm looking for - evidence of ability to think about a problem. I don't really care if they can write a string match, that's just something to hang the discussion off.
Paul
+1  A: 

I think the following should achieve what you intend to do -

int main(int argc, char *argv[]) {

string A, B;
size_t pos;

while (true) 
{
    cout << endl << "Enter string: ";
    getline(cin,A);
    cout << "Enter substring to find: ";
    getline(cin,B);
    if ((A.size() > 0) && (B.size() > 0)) 
    {
        cout << "\"" << B << "\" is";
        if ((pos = find(A,B)) == string::npos) 
        {
            cout << " not";
        }
        cout << " a substring of \"" << A<< "\"";
        if (pos != string::npos) 
        {
            cout << ", found at index " << pos;
        }
        cout << endl;
    }
}
return 0;

}

takyon
That is C++ while the question says C, but otherwise looks good, though my guess is that they want you to implement your own version of find(), just to show you can do it. Cheers.
Tony
No. You didn't write any algorithm at all, you just used `find`. That would be quite a few demerits in my book.
JoshD
@JoshD: the question as posted asks for a program not an algorithm... an interviewer's always free to ask more precisely for what they actually wanted, but it's unfair to penalise this when the spec. was unclear.
Tony
@Tony: I can see that side, though I would argue that such a question strongly implies that an algorithm is desired. For what it's worth, I didn't mark down the question. As you said, it isn't strictly wrong.
JoshD
A: 

If you're interested, have a look at Michael Abrash's chapter(PDF) on string searching in his "Graphics Programming Black Book". The rest of the book can be freely accessed here.

He starts out with a nice routine in C, then improves it and implements it in assembly. Although not really in the scope of the interview question, I think most interviewers would be impressed with an explanation of how to optimise your answer.

Dijkstra
A: 

The question doesn't specify a result for if B does not occur in A, nor does it require the position of the occurrence. So it is safe to assume that A contains B. Hence the program should return string B, as the value of the first occurrence of B in A will be equal to B.

I believe much of Windows NT's POSIX compliance was built on similar principles.

Pete Kirkham