replacement for stristr

S

Siemel Naran

There is a function stricmp that compares two strings without case
sensitivity.

We have strstr that searches for a substring, taking case into account.

Is there a function stristr? If not, how to write one?

Thanks.
 
J

John Carson

Siemel Naran said:
There is a function stricmp that compares two strings without case
sensitivity.

We have strstr that searches for a substring, taking case into
account.

Is there a function stristr? If not, how to write one?

Thanks.


stricmp is a non-standard (presumably Microsoft) extension (now designated
_stricmp to indicate that fact).

I think that you will need to convert the string to all uppercase or all
lowercase first, and then call strstr (there are Microsoft functions for
this --- called _strupr and _strlwr --- but beware that they will overwrite
the original string, so you may need to first make a copy).
 
T

Thomas Matthews

John said:
stricmp is a non-standard (presumably Microsoft) extension (now designated
_stricmp to indicate that fact).

I think that you will need to convert the string to all uppercase or all
lowercase first, and then call strstr (there are Microsoft functions for
this --- called _strupr and _strlwr --- but beware that they will overwrite
the original string, so you may need to first make a copy).

One could compare each character in a C-style character
using the std::toupper or std::tolower functions to make
sure you are comparing case insensitive:

int my_stricmp(const char * a
const char * b)
{
while (*a != '\0' && *b != '\0')
{
if (toupper(*a) != toupper(*b))
return (toupper(*a) < toupper(*b)) ? -1 : 1;
++a;
++b;
}
if (*a == *b)
return 0;
return (*a == '\0') ? -1 : 1;
}

One could use traits and locale to create an
uppercase only (or lowercase only) string type
using std::basic_string<>.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
D

Default User

Thomas said:
John Carson wrote:
One could compare each character in a C-style character
using the std::toupper or std::tolower functions to make
sure you are comparing case insensitive:

int my_stricmp(const char * a
const char * b)


He wanted a case-insensitive strstr(), not strcmp().



Brian Rodenborn
 
S

Siemel Naran

lilburne said:
Siemel Naran wrote:

Write a test. Things to test are what happens if either input are an
empty string "". The substring length is greater then the string to be
searched. That the substring "aaa" is in the middle of the string
"baaabbb". That a false start can resync "abc" "bababcd".

Thanks. Anything else you can think of?
 
L

Les Matheson

Siemel,

Here's a version I wrote many years ago and still use. It has a lot
of miles on it, so I'm reasonably confident it won't blow up:

LPSTR stristr(const char *pszText, const char *pszSub)
{
if (!pszText || !pszSub || !*pszText || !*pszSub)
return 0;
int nLen(int(strlen(pszSub)));
const char test(toupper(*pszSub));
while (*pszText)
{
if (toupper(*pszText)==test)
{
if (strnicmp(pszText,pszSub,nLen)==0)
return LPSTR(pszText);
}
pszText++;
}
return 0;
}

Good luck,
Les Matheson
Integral Concepts, Inc.
http://www.ivsds.com
 
D

Default User

Les said:
Siemel,

Here's a version I wrote many years ago and still use. It has a lot
of miles on it, so I'm reasonably confident it won't blow up:

LPSTR stristr(const char *pszText, const char *pszSub)
{
if (!pszText || !pszSub || !*pszText || !*pszSub)
return 0;
int nLen(int(strlen(pszSub)));
const char test(toupper(*pszSub));
while (*pszText)
{
if (toupper(*pszText)==test)
{
if (strnicmp(pszText,pszSub,nLen)==0)
return LPSTR(pszText);
}
pszText++;
}
return 0;
}


I realize that this is cross-posted to a windows newsgroup, but in
general it's better to use standard constructs whenever possible to
improve portability. The problem at hand can be solved without use of
non-standard functions like strincmp() or proprietary types like LPSTR.

Also stristr is a reserved identifier.



Brian Rodenborn
 
D

Default User

Makhno said:
reserved where?

In the ISO C and C++ standards. All identifiers starting with str and
followed by a lowercase letter are reserved for future library
directions.

From the C99 draft standard:

7.26 Future library directions

[#1] The following names are grouped under individual
headers for convenience. All external names described below
are reserved no matter what headers are included by the
program.


7.26.10 General utilities <stdlib.h>

[#1] Function names that begin with str and a lowercase
letter (possibly followed by any combination of digits,
letters, and underscore) may be added to the declarations in
the <stdlib.h> header.



Brian Rodenborn
 
L

Les Matheson

I realize that this is cross-posted to a windows newsgroup, but in
general it's better to use standard constructs whenever possible to
improve portability. The problem at hand can be solved without use of
non-standard functions like strincmp() or proprietary types like LPSTR.

Also stristr is a reserved identifier.

Good point, and thanks for the follow-up.

Les Matheson
Integral Concepts, Inc.
http://www.ivsds.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,436
Latest member
MaxD

Latest Threads

Top