Re: Evaluation of C program



#include <stdio.h>

Your entire find_substring() function could be replaced with a
single call to strstr();

Yep. This was an exercise so I didn't look, but good to know.

char *find_substring(char *substring, char *string) {
int index_string, index_substring;
for (index_string = 0; string[index_string] != 0; index_string++) {

strings can be longer than an 'int' can hold. Check out size_t

Good point.

index_substring = 0;
do {
if (substring[index_substring] == 0)
goto success;

goto should rarely be used. Consider using "break" here.

if (string[index_string + index_substring] !=
substring[index_substring])
goto next;

goto should rarely be used. Consider using "continue" here.

I don't see the harm in using goto.. it does the job. :-)

} while (++index_substring);

The only way that ++index_substring could be false in that test is
if the int representation overflowed. This suggests that you are using
the wrong control structure; consider using a for().

Well, it will always goto using one of the two statements above it, so it works. But it should be a long, yes.

success:
return &string[index_string];
next: ;
}
return 0;

Returning 0 is not wrong here, but it would be more readable
if you were to use NULL instead of 0.

Yep.

}

int main() {

int main(void) {

if(find_substring("test", "this is a test"))
printf("Found test in string!\n");

If you do not find the substring, then you print nothing, and
won't know what happened.

Yeah..

return 0;

Returning 0 indicates success, but it could be argued that if you
do not find the substring then your program has failed and so
returning EXIT_FAILURE (from <stdlib.h>) would be more appropriate
in that instance.

}


Could this be done better/differently?

Suppose you don't find a match the first iteration through
because the 10th character in the trial substring does not match
the 10th character in the input string. You then loop back
and start testing from the substring again from the second
character of the input string -- ignoring all the information
you could have gleened by paying attention to what was in
offsets 1 thru 9 that you have already looked at.

Suppose for example that what you found at the 10th character at the
input string was an 'X', and there are no occurances of 'X' in the
trial substring. A moment's reflection would show you that in
such an instance you would be able to skip testing for the
substring as starting from 1 thru 9, since that 'X' will still be
there at offset 9 blocking any possible match.

What this tells you is that there are algorithms which can be
much faster than your search algorithm. Indeed, there is an entire
literature on search algorithms. Some are probably mentioned in the C
FAQ or by googling for "string search algorithms".

OK. This was a bit beyond what I was looking to figure out, but good to know I guess.

Thanks. :)

-Morten
.



Relevant Pages

  • Re: Evaluation of C program
    ... substring and return pointer to it function and tests for it.. ... the 10th character in the input string. ... much faster than your search algorithm. ...
    (comp.lang.c)
  • Re: Evaluation of C program
    ... substring and return pointer to it function and tests for it.. ... strings can be longer than an 'int' can hold. ... the 10th character in the input string. ... much faster than your search algorithm. ...
    (comp.lang.c)
  • Re: Evaluation of C program
    ... substring and return pointer to it function and tests for it.. ... strings can be longer than an 'int' can hold. ... the 10th character in the input string. ...
    (comp.lang.c)