Comments about your code:
I believe that you would rename it to something like strstr
if you could.
You should accept pointers to const char
, since you don't modify them.
You should initialize i
and n
right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
using int
for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use size_t
instead, because it is guaranteed that size_t
will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript ([]
) operator.
Loop could be rewritten in other, more readable and straightforward form, one of which is shown in suggested implementation. I know that you would like to have high performance, but I don't think that it is that important in this situation. May be with more context of the usage of the algorithm I could suggest particular algorithm (there are quite a few of them). The best in my opinion is Boyer–Moore string search algorithm.
Suggested implementation:
I think that strncmp
should be used. I know that including string.h
header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
{
size_t substrlen = strlen(substr);
while (*text != 0)
{
if (strncmp(text, substr, substrlen) == 0)
{
return text;
}
++text;
}
return NULL;
}
The performance gain from the data being const depends on physical constness and the compiler. Further reading Further reading.
Comments about your code:
I believe that you would rename it to something like strstr
if you could.
You should accept pointers to const char
, since you don't modify them.
You should initialize i
and n
right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
using int
for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use size_t
instead, because it is guaranteed that size_t
will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript ([]
) operator.
Loop could be rewritten in other, more readable and straightforward form, one of which is shown in suggested implementation. I know that you would like to have high performance, but I don't think that it is that important in this situation. May be with more context of the usage of the algorithm I could suggest particular algorithm (there are quite a few of them). The best in my opinion is Boyer–Moore string search algorithm.
Suggested implementation:
I think that strncmp
should be used. I know that including string.h
header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
{
size_t substrlen = strlen(substr);
while (*text != 0)
{
if (strncmp(text, substr, substrlen) == 0)
{
return text;
}
++text;
}
return NULL;
}
The performance gain from the data being const depends on physical constness and the compiler. Further reading.
Comments about your code:
I believe that you would rename it to something like strstr
if you could.
You should accept pointers to const char
, since you don't modify them.
You should initialize i
and n
right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
using int
for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use size_t
instead, because it is guaranteed that size_t
will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript ([]
) operator.
Loop could be rewritten in other, more readable and straightforward form, one of which is shown in suggested implementation. I know that you would like to have high performance, but I don't think that it is that important in this situation. May be with more context of the usage of the algorithm I could suggest particular algorithm (there are quite a few of them). The best in my opinion is Boyer–Moore string search algorithm.
Suggested implementation:
I think that strncmp
should be used. I know that including string.h
header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
{
size_t substrlen = strlen(substr);
while (*text != 0)
{
if (strncmp(text, substr, substrlen) == 0)
{
return text;
}
++text;
}
return NULL;
}
The performance gain from the data being const depends on physical constness and the compiler. Further reading.
Comments about your code:
I believe that you would rename it to something like strstr
if you could.
You should accept pointers to const char
, since you don't modify them.
You should initialize i
and n
right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
using int
for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use size_t
instead, because it is guaranteed that size_t
will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript ([]
) operator.
Loop could be rewritten in other, more readable and straightforward form, one of which is shown in suggested implementation. I know that you would like to have high performance, but I don't think that it is that important in this situation. May be with more context of the usage of the algorithm I could suggest particular algorithm (there are quite a few of them). The best in my opinion is Boyer–Moore string search algorithm.
Suggested implementation:
I think that strncmp
should be used. I know that including string.h
header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
{
size_t substrlen = strlen(substr);
while (*text != 0)
{
if (strncmp(text, substr, substrlen) == 0)
{
return text;
}
++text;
}
return NULL;
}
The performance gain from the data being const depends on physical constness and the compiler. Further reading.
Comments about your code:
I believe that you would rename it to something like strstr
if you could.
You should accept pointers to const char
, since you don't modify them.
You should initialize i
and n
right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
using int
for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use size_t
instead, because it is guaranteed that size_t
will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript ([]
) operator.
Loop could be rewritten in other, more readable and straightforward form. I know that you would like to have high performance, but I don't think that it is that important in this situation. May be with more context of the usage of the algorithm I could suggest particular algorithm (there are quite a few of them). The best in my opinion is Boyer–Moore string search algorithm.
Suggested implementation:
I think that strncmp
should be used. I know that including string.h
header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
{
size_t substrlen = strlen(substr);
while (*text != 0)
{
if (strncmp(text, substr, substrlen) == 0)
{
return text;
}
++text;
}
return NULL;
}
The performance gain from the data being const depends on physical constness and the compiler. Further reading.
Comments about your code:
I believe that you would rename it to something like strstr
if you could.
You should accept pointers to const char
, since you don't modify them.
You should initialize i
and n
right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
using int
for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use size_t
instead, because it is guaranteed that size_t
will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript ([]
) operator.
Loop could be rewritten in other, more readable and straightforward form, one of which is shown in suggested implementation. I know that you would like to have high performance, but I don't think that it is that important in this situation. May be with more context of the usage of the algorithm I could suggest particular algorithm (there are quite a few of them). The best in my opinion is Boyer–Moore string search algorithm.
Suggested implementation:
I think that strncmp
should be used. I know that including string.h
header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
{
size_t substrlen = strlen(substr);
while (*text != 0)
{
if (strncmp(text, substr, substrlen) == 0)
{
return text;
}
++text;
}
return NULL;
}
The performance gain from the data being const depends on physical constness and the compiler. Further reading.
- 9.7k
- 3
- 34
- 73
Comments about your code:
I believe that you would rename it to something like strstr
if you could.
You should accept pointers to const char
, since you don't modify them.
The algorithm can be written to use less memory and possibly incur less overhead.
You should initialize i
and n
right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
using int
for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use size_t
instead, because it is guaranteed that size_t
will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript ([]
) operator.
Loop could be rewritten in other, more readable and straightforward form. I know that you would like to have high performance, but I don't think that it is that important in this situation. If you really want to gain performanceMay be with more context of the usage of the algorithm I could suggest you to implementparticular algorithm (there are quite a few of them ). The best in my opinion is Boyer–Moore string search algorithm.
Suggested implementation:
I think that strncmp
should be used. I know that including string.h
header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
{
size_t substrlen = strlen(substr);
while (*text != 0)
{
if (strncmp(text, substr, substrlen) == 0)
{
return text;
}
++text;
}
return NULL;
}
The performance gain from the data being const depends on physical constness and the compiler. Further reading.
Comments about your code:
I believe that you would rename it to something like strstr
if you could.
You should accept pointers to const char
, since you don't modify them.
The algorithm can be written to use less memory and possibly incur less overhead.
You should initialize i
and n
right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
using int
for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use size_t
instead, because it is guaranteed that size_t
will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript ([]
) operator.
Loop could be rewritten in other, more readable and straightforward form. I know that you would like to have high performance, but I don't think that it is that important in this situation. If you really want to gain performance I suggest you to implement Boyer–Moore string search algorithm.
Suggested implementation:
I think that strncmp
should be used. I know that including string.h
header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
{
size_t substrlen = strlen(substr);
while (*text != 0)
{
if (strncmp(text, substr, substrlen) == 0)
{
return text;
}
++text;
}
return NULL;
}
The performance gain from the data being const depends on physical constness and the compiler. Further reading.
Comments about your code:
I believe that you would rename it to something like strstr
if you could.
You should accept pointers to const char
, since you don't modify them.
You should initialize i
and n
right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
using int
for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use size_t
instead, because it is guaranteed that size_t
will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript ([]
) operator.
Loop could be rewritten in other, more readable and straightforward form. I know that you would like to have high performance, but I don't think that it is that important in this situation. May be with more context of the usage of the algorithm I could suggest particular algorithm (there are quite a few of them ). The best in my opinion is Boyer–Moore string search algorithm.
Suggested implementation:
I think that strncmp
should be used. I know that including string.h
header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
{
size_t substrlen = strlen(substr);
while (*text != 0)
{
if (strncmp(text, substr, substrlen) == 0)
{
return text;
}
++text;
}
return NULL;
}
The performance gain from the data being const depends on physical constness and the compiler. Further reading.