Re: Evaluation of C program
- From: roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson)
- Date: Thu, 16 Feb 2006 18:56:02 +0000 (UTC)
In article <1140113852.635717.152500@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
<morphex@xxxxxxxxx> wrote:
I'm a python programmer that's started to play a bit with C as I'll
probably have to make C extensions eventually.. I made this little
program that I'd like to get feedback on, it's basically a find
substring and return pointer to it function and tests for it..
Could this be done better/differently? Is anything fundamentally
wrong?
#include <stdio.h>
Your entire find_substring() function could be replaced with a
single call to strstr();
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
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.
} 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().
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.
}
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.
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".
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
.
- Follow-Ups:
- Re: Evaluation of C program
- From: micans
- Re: Evaluation of C program
- From: Fred Kleinschmidt
- Re: Evaluation of C program
- From: Morten W. Petersen
- Re: Evaluation of C program
- References:
- Evaluation of C program
- From: morphex
- Evaluation of C program
- Prev by Date: get_double input function (getchar+scanf)
- Next by Date: Re: How to tokenize string without using strtok
- Previous by thread: Evaluation of C program
- Next by thread: Re: Evaluation of C program
- Index(es):
Relevant Pages
|