473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

replacement for stristr

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.

--
+++++++++++
Siemel Naran

Jul 22 '05 #1
14 18551
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:%H********************@bgtnsc05-news.ops.worldnet.att.net
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).
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
John Carson wrote:
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:%H********************@bgtnsc05-news.ops.worldnet.att.net
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).


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

Jul 22 '05 #3
Siemel Naran wrote:

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?

Always a good bet is snippets.org:
http://c.snippets.org/snip_lister.php?fname=stristr.c

Brian Rodenborn
Jul 22 '05 #4
Thomas Matthews wrote:

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
Jul 22 '05 #5
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message

Thanks. I can extend this idea to write my own stristr.
return (toupper(*a) < toupper(*b)) ? -1 : 1;


Out of curiosity, which is faster -- the one above or

return toupper(*a) - toupper(*b);

I imagine it varies platform by platform?

--
+++++++++++
Siemel Naran
Jul 22 '05 #6
"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
Jul 22 '05 #7


Siemel Naran wrote:

I wrote the following which seems to work, but would like confirmation if it
is correct.


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".

Jul 22 '05 #8
"lilburne" <li******@godzilla.com> wrote in message news:c1n27p$1kpdl9$1@ID-
Siemel Naran wrote:

I wrote the following which seems to work, but would like confirmation if it is correct.


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?

--
+++++++++++
Siemel Naran
Jul 22 '05 #9
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
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<Dd*********************@bgtnsc04-news.ops.worldnet.att.net>...
"lilburne" <li******@godzilla.com> wrote in message news:c1n27p$1kpdl9$1@ID-
Siemel Naran wrote:

I wrote the following which seems to work, but would like confirmation if it is correct.


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?

Jul 22 '05 #10
Les Matheson wrote:

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
Jul 22 '05 #11
> Also stristr is a reserved identifier.

reserved where?
Jul 22 '05 #12

"Makhno" <ro**@127.0.0.1> wrote in message news:c2**********@newsg2.svr.pol.co.uk...
Also stristr is a reserved identifier.


reserved where?

The C standard reserves identifiers that begin with str and another lowercase letter
in the global namespace.

Jul 22 '05 #13
Makhno wrote:
Also stristr is a reserved identifier.


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
Jul 22 '05 #14
> 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
Jul 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul Miller | last post by:
We've run into minidom's inabilty to handle large (20+MB) XML files, and need a replacement that can handle it. Unfortunately, we're pretty dependent on a DOM, so a pulldom or SAX replacement is...
53
by: Kerberos | last post by:
I followed Dan Cederholm's image replacement tutorial, to replace a header tag by a logo. The h1 is clickable if no CSS is applied but it I replace it by the logo, the area isn't clickable anymore...
3
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject)...
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
20
by: Paul D. Boyle | last post by:
Hi all, There was a recent thread in this group which talked about the shortcomings of fgets(). I decided to try my hand at writing a replacement for fgets() using fgetc() and realloc() to read...
3
by: Jeffrey D. Gordon | last post by:
I'm wanting to replace Field Values in an existing PDF, I've done this with PHP by doing a replace in the file. I've been able to read the file in a byte array in c# but all my attempts to...
3
by: chris | last post by:
Hallo, I am in need of a replacement for the Microsoft Visual Studio .NET. The reason is quiet simple. I develop forms which are used on different microsoft windows platform, and one...
1
by: lawrence k | last post by:
Want to replace the limit clause in a query, but can't get it right. What's wrong with this: $pattern = "(.*)limit (.*)"; $replacement = '$1'; $replacement .= "LIMIT $limit"; $replacement .=...
3
by: =?Utf-8?B?RHVrZSAoQU4yNDcp?= | last post by:
I've added a web deployment project and want to use the config section replacement but I'm obviously not understanding something. I have set up an alternate appSettings file,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.