Author |
Message |
Ravi Ud #1 / 22
|
 Case Insensitive strstr !!
Hi.. The function strstr - [ char *strstr( const char *string, const char *subString ); ] is used to find a substring, in a given string. Well this function is CASE SENSITIVE ( subString ). Can anybody in here help me with a similar function which returns a pointer to the first occurrence of substring where in it does in a CASE INSENSITIVE Manner. ( i.e. it should ignore the case of the input SubString and do its job. ) Thanks for the support - Ravi
|
Wed, 09 Feb 2005 03:52:36 GMT |
|
 |
Ryan Henness #2 / 22
|
 Case Insensitive strstr !!
Quote:
> Hi.. > The function strstr - > [ char *strstr( const char *string, const char *subString ); ] > is used to find a substring, in a given string. > Well this function is CASE SENSITIVE ( subString ). > Can anybody in here help me with a similar function which returns a > pointer to the first occurrence of substring where in it does in a > CASE INSENSITIVE Manner. > ( i.e. it should ignore the case of the input SubString and do its > job. ) > Thanks for the support > - Ravi
Someone asked a question in here a week or so back about a case-insensitive strcmp(). The answer here is the same as there: code strstr() as normal, except pump each character to tolower() [or toupper()] first. Oh, and don't forget to cast each character to unsigned before passing it to to*(). And don't lie to your compiler, or Eric Sosman will notice ;P Hope that helps, Ryan.
|
Wed, 09 Feb 2005 03:52:40 GMT |
|
 |
nrk #3 / 22
|
 Case Insensitive strstr !!
Quote:
> Hi.. > The function strstr - > [ char *strstr( const char *string, const char *subString ); ] > is used to find a substring, in a given string. > Well this function is CASE SENSITIVE ( subString ). > Can anybody in here help me with a similar function which returns a > pointer to the first occurrence of substring where in it does in a > CASE INSENSITIVE Manner. > ( i.e. it should ignore the case of the input SubString and do its > job. ) > Thanks for the support > - Ravi
You could re-use the strstr function by creating entirely lowercase/uppercase copies of the two strings and then calling strstr on these copies instead of the original string. If you feel the urge to optimize, remember the words of the great master: "Pre-mature optimisation is the root of all evil". ;-) -nrk.
|
Wed, 09 Feb 2005 04:41:03 GMT |
|
 |
jose.. #4 / 22
|
 Case Insensitive strstr !!
Quote:
> Hi.. > The function strstr - > [ char *strstr( const char *string, const char *subString ); ] > is used to find a substring, in a given string. > Well this function is CASE SENSITIVE ( subString ). > Can anybody in here help me with a similar function which returns a > pointer to the first occurrence of substring where in it does in a > CASE INSENSITIVE Manner. > ( i.e. it should ignore the case of the input SubString and do its > job. )
man strstr(3) has: char * strcasestr(const char *big, const char *little); I don't have a copy of the standard handy but FreeBSD claims it's part of the 'Standard C Library'. Joe
|
Wed, 09 Feb 2005 04:46:29 GMT |
|
 |
jose.. #5 / 22
|
 Case Insensitive strstr !!
Quote:
> man strstr(3) has: > char * > strcasestr(const char *big, const char *little); > I don't have a copy of the standard handy but FreeBSD claims it's > part of the 'Standard C Library'.
Ok. I'm on crack. This isn't on my SunOS box or a RedHat box. I was wrong. Joe
|
Wed, 09 Feb 2005 04:52:38 GMT |
|
 |
Default Use #6 / 22
|
 Case Insensitive strstr !!
Quote:
> Hi.. > The function strstr - > [ char *strstr( const char *string, const char *subString ); ] > is used to find a substring, in a given string. > Well this function is CASE SENSITIVE ( subString ). > Can anybody in here help me with a similar function which returns a > pointer to the first occurrence of substring where in it does in a > CASE INSENSITIVE Manner. > ( i.e. it should ignore the case of the input SubString and do its > job. )
There is no standard function to do that. You must either use a platform-specific solution (stricmp() is a typical name, check your documenation) or write your own. It's not too difficult. You could probably find an implementation via google. Brian Rodenborn
|
Wed, 09 Feb 2005 04:54:30 GMT |
|
 |
Malcol #7 / 22
|
 Case Insensitive strstr !!
Quote:
> Hi.. > The function strstr - > [ char *strstr( const char *string, const char *subString ); ] > is used to find a substring, in a given string. > Can anybody in here help me with a similar function which returns a > pointer to the first occurrence of substring where in it does in a > CASE INSENSITIVE Manner. > ( i.e. it should ignore the case of the input SubString and do its > job. )
Try this (untested). char *mystristr(const char *str, const char *substr) { int len; len = strlen(substr); while(*str) { if(!mystrincmp(str, substr, len)) return str; } return 0; Quote: }
int mystrincmp(const char *s1, const char *s2, int len) { int i; for(i=0;i<len;i++) if(s1[i] != s2[i]) return s1[i] - s2[i]; return 0; Quote: }
|
Wed, 09 Feb 2005 05:25:18 GMT |
|
 |
Malcol #8 / 22
|
 Case Insensitive strstr !!
Quote:
> > Hi.. > > The function strstr - > > [ char *strstr( const char *string, const char *subString ); ] > > is used to find a substring, in a given string. > > Can anybody in here help me with a similar function which returns a > > pointer to the first occurrence of substring where in it does in a > > CASE INSENSITIVE Manner. > > ( i.e. it should ignore the case of the input SubString and do its > > job. ) > Try this (untested). > char *mystristr(const char *str, const char *substr) > { > int len; > len = strlen(substr); > while(*str) > { > if(!mystrincmp(str, substr, len)) > return str; > } > return 0; > } > int mystrincmp(const char *s1, const char *s2, int len) > { > int i; > for(i=0;i<len;i++) duh > if(tolower(s1[i]) != tolower(s2[i])) > return s1[i] - s2[i]; > return 0; > }
|
Wed, 09 Feb 2005 05:29:39 GMT |
|
 |
nrk #9 / 22
|
 Case Insensitive strstr !!
Quote:
>>>Hi.. >>>The function strstr - >>>[ char *strstr( const char *string, const char *subString ); ] >>>is used to find a substring, in a given string. >>>Can anybody in here help me with a similar function which returns a >>>pointer to the first occurrence of substring where in it does in a >>>CASE INSENSITIVE Manner. >>>( i.e. it should ignore the case of the input SubString and do its >>>job. ) >>Try this (untested). >>char *mystristr(const char *str, const char *substr) >>{ >> int len; >> len = strlen(substr); >> while(*str) >> { >> if(!mystrincmp(str, substr, len)) >> return str; >> }
Huh?? Quote: >> return 0; >>} >>int mystrincmp(const char *s1, const char *s2, int len) >>{ >> int i; >> for(i=0;i<len;i++) > duh >> if(tolower(s1[i]) != tolower(s2[i])) >> return s1[i] - s2[i]; >> return 0; >>}
Can you explain how the above code does what strstr is supposed to do? (Looks like you missed an increment inside your while loop). -nrk.
|
Wed, 09 Feb 2005 05:51:20 GMT |
|
 |
Default Use #10 / 22
|
 Case Insensitive strstr !!
Quote:
> There is no standard function to do that. You must either use a > platform-specific solution (stricmp() is a typical name
Every get half-way through your answer and forget the original question? That's what I did. He wanted strstr() not strcmp(). Brian Rodenborn
|
Wed, 09 Feb 2005 05:43:12 GMT |
|
 |
bjarke.. #11 / 22
|
 Case Insensitive strstr !!
Quote:
> > man strstr(3) has: > > char * > > strcasestr(const char *big, const char *little); > > I don't have a copy of the standard handy but FreeBSD claims it's > > part of the 'Standard C Library'. > Ok. I'm on crack. This isn't on my SunOS box or a RedHat box. I > was wrong.
From string.h on RedHat 7.3: #ifdef __USE_GNU /* Similar to `strstr' but this function ignores the case of both strings. */ extern char *strcasestr (__const char *__haystack, __const char *__needle) __THROW __attribute_pure__; #endif From string.h on Irix: #if _SGIAPI && _NO_ANSIMODE extern int ffs(int); /* Case-insensitive comparision routines from 4.3BSD. */ extern int strcasecmp(const char *, const char *); extern int strncasecmp(const char *, const char *, size_t); #endif /* _SGIAPI */ From string.h on SunOs: #if defined(__EXTENSIONS__) || (__STDC__ == 0 && \ !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) extern char *strsignal(int); extern int ffs(int); extern int strcasecmp(const char *, const char *); extern int strncasecmp(const char *, const char *, size_t); #endif /* defined(__EXTENSIONS__)... */ So I think it exists on most platforms. Bjarke -- "Always go to other people's funerals, otherwise they won't come to yours." - Yogi Berra
|
Wed, 09 Feb 2005 15:41:16 GMT |
|
 |
Micah Cowa #12 / 22
|
 Case Insensitive strstr !!
Quote:
> > > man strstr(3) has: > > > char * > > > strcasestr(const char *big, const char *little); > > > I don't have a copy of the standard handy but FreeBSD claims it's > > > part of the 'Standard C Library'. > > Ok. I'm on crack. This isn't on my SunOS box or a RedHat box. I > > was wrong. > From string.h on RedHat 7.3: > #ifdef __USE_GNU > /* Similar to `strstr' but this function ignores the case of both strings. */ > extern char *strcasestr (__const char *__haystack, __const char *__needle) > __THROW __attribute_pure__; > #endif > From string.h on Irix: > #if _SGIAPI && _NO_ANSIMODE > extern int ffs(int); > /* Case-insensitive comparision routines from 4.3BSD. */ > extern int strcasecmp(const char *, const char *); > extern int strncasecmp(const char *, const char *, size_t); > #endif /* _SGIAPI */ > From string.h on SunOs: > #if defined(__EXTENSIONS__) || (__STDC__ == 0 && \ > !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) > extern char *strsignal(int); > extern int ffs(int); > extern int strcasecmp(const char *, const char *); > extern int strncasecmp(const char *, const char *, size_t); > #endif /* defined(__EXTENSIONS__)... */ > So I think it exists on most platforms.
Why? Neither the Irix nor SunOs citations above have it. In any case, it is neither part of the C standard, nor part of the POSIX standard. It shouldn't be relied upon. Right your own, if you really need it. -Micah
|
Wed, 09 Feb 2005 17:00:30 GMT |
|
 |
Glynn #13 / 22
|
 Case Insensitive strstr !!
Ravi......try this: /*****************************/ #include <stdio.h> #include <string.h> #include <ctype.h> char *strcasestr( char *szSrc, char *szFind ) { int i, k, nFind, nSrc; if( !szSrc || !szSrc[0] || !szFind || !szFind[0] ) { return(0); } nSrc= strlen(szSrc); nFind= strlen(szFind); for( i=k=0 ; k<nFind && i<nSrc ; i++, k++ ) { if( toupper(szSrc[i]) != toupper(szFind[k]) ) { i -= k; k = -1; } } if( k==nFind ) { return(szSrc + i-nFind); } return(0); Quote: }
/*****************************/ ~Glynne
Quote: > Hi.. > The function strstr - > [ char *strstr( const char *string, const char *subString ); ] > is used to find a substring, in a given string. > Well this function is CASE SENSITIVE ( subString ). > Can anybody in here help me with a similar function which returns a > pointer to the first occurrence of substring where in it does in a > CASE INSENSITIVE Manner. > ( i.e. it should ignore the case of the input SubString and do its > job. ) > Thanks for the support > - Ravi
|
Thu, 10 Feb 2005 12:49:26 GMT |
|
 |
Ravi Ud #14 / 22
|
 Case Insensitive strstr !!
Quote:
> > > > man strstr(3) has: > > > > char * > > > > strcasestr(const char *big, const char *little); > > > > I don't have a copy of the standard handy but FreeBSD claims it's > > > > part of the 'Standard C Library'. > > > Ok. I'm on crack. This isn't on my SunOS box or a RedHat box. I > > > was wrong. > > From string.h on RedHat 7.3: > > #ifdef __USE_GNU > > /* Similar to `strstr' but this function ignores the case of both strings. */ > > extern char *strcasestr (__const char *__haystack, __const char *__needle) > > __THROW __attribute_pure__; > > #endif > > From string.h on Irix: > > #if _SGIAPI && _NO_ANSIMODE > > extern int ffs(int); > > /* Case-insensitive comparision routines from 4.3BSD. */ > > extern int strcasecmp(const char *, const char *); > > extern int strncasecmp(const char *, const char *, size_t); > > #endif /* _SGIAPI */ > > From string.h on SunOs: > > #if defined(__EXTENSIONS__) || (__STDC__ == 0 && \ > > !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) > > extern char *strsignal(int); > > extern int ffs(int); > > extern int strcasecmp(const char *, const char *); > > extern int strncasecmp(const char *, const char *, size_t); > > #endif /* defined(__EXTENSIONS__)... */ > > So I think it exists on most platforms. > Why? Neither the Irix nor SunOs citations above have it. > In any case, it is neither part of the C standard, nor part of the > POSIX standard. It shouldn't be relied upon. Right your own, if you > really need it. > -Micah
well just trying..but want to find the best optimum solution. Standard 'C' ofcourse !!
|
Thu, 10 Feb 2005 19:20:50 GMT |
|
 |
Micah Cowa #15 / 22
|
 Case Insensitive strstr !!
Quote:
> In any case, it is neither part of the C standard, nor part of the > POSIX standard. It shouldn't be relied upon. Right your own, if you
^^^^^ Quote: > really need it.
Doh! So embarassing... -Micah
|
Thu, 10 Feb 2005 22:01:01 GMT |
|
|