"Default User" <fi********@boeing.com.invalid> wrote in message
Siemel Naran wrote:
Is there a function stristr? If not, how to write one?
Always a good bet is snippets.org:
http://c.snippets.org/snip_lister.php?fname=stristr.c
Thanks. This is a nice link. So I have to write my own? Searching in the
standard I find there is a std::search.
template <class charT>
truct eiq {
bool operator()(charT c1, charT c2) const {
return toupper(c1)==toupper(c2);
}
};
const char * stristr(const char * s1, const char * s2) {
const char * s1end = s1+strlen(s1);
const char * out = std::search(s1, s1end, s2, s2+strlen(s2),
eiq<char>());
if (out == s1end) return NULL;
return out;
}
I think the calls to std::strlen may make lose efficiency because we scan
each of the entire strings once to find the length.
I wrote the following which seems to work, but would like confirmation if it
is correct.
template <class charT>
const charT * enhanced::stristr(const charT * s1, const charT *const
s2start)
{
using std::toupper;
const charT * s2startplus1 = s2start + 1u;
const charT * s2 = s2startplus1;
const charT c2start = toupper(*s2start);
charT c2 = c2start;
for ( ; ; ++s1)
{
const charT c1 = toupper(*s1);
if (!c1) break;
if (c1 == c2)
{
c2 = toupper(*s2);
if (!c2) return s1-(s2-s2start-1u);
++s2;
}
else if (s2 != s2startplus1)
{
s2 = s2startplus1;
c2 = c2start;
}
}
return NULL;
}
--
+++++++++++
Siemel Naran