M
morphex
Hi,
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>
char *find_substring(char *substring, char *string) {
int index_string, index_substring;
for (index_string = 0; string[index_string] != 0; index_string++) {
index_substring = 0;
do {
if (substring[index_substring] == 0)
goto success;
if (string[index_string + index_substring] !=
substring[index_substring])
goto next;
} while (++index_substring);
success:
return &string[index_string];
next: ;
}
return 0;
}
int main() {
if(find_substring("test", "this is a test"))
printf("Found test in string!\n");
return 0;
}
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>
char *find_substring(char *substring, char *string) {
int index_string, index_substring;
for (index_string = 0; string[index_string] != 0; index_string++) {
index_substring = 0;
do {
if (substring[index_substring] == 0)
goto success;
if (string[index_string + index_substring] !=
substring[index_substring])
goto next;
} while (++index_substring);
success:
return &string[index_string];
next: ;
}
return 0;
}
int main() {
if(find_substring("test", "this is a test"))
printf("Found test in string!\n");
return 0;
}