I have two strings. Lets say `
str1="One Two Three";
and
str2="two";
I would like to know if there is any function that checks for a match of the second string in the first one,and returns me a pointer to the first occurrence, something like strstr()
, but which doesn't treat the same letter, upper or lowercase, as two different characters.
For my example, the function should find a match for str2
in the first string, despite the uppercase "T"
, of "Two"
.
This function is case-sensitive. For case-insensitive searches, use stristr().
The strstr() function returns a pointer to the beginning of the first occurrence of string2 in string1. If string2 does not appear in string1, the strstr() function returns NULL. If string2 points to a string with zero length, the strstr() function returns string1.
Note: The strchr() function is case sensitive.
From the manpage for strstr
:
STRSTR(3) Linux Programmer's Manual STRSTR(3)
NAME
strstr, strcasestr - locate a substring
SYNOPSIS
#include <string.h>
char *strstr(const char *haystack, const char *needle);
#define _GNU_SOURCE
#include <string.h>
char *<b><u>strcasestr</u></b>(const char *haystack, const char *needle);
DESCRIPTION
The strstr() function finds the first occurrence of the substring needle in
the string haystack. The terminating '\0' characters are not compared.
<b>The strcasestr() function is like strstr(3), but ignores the case of both
arguments.</b>
RETURN VALUE
These functions return a pointer to the beginning of the substring, or NULL if
the substring is not found.
So what you're looking for is strcasestr
.
While some compiler's C libraries include extensions with case insensitive versions of the standard string functions, such as GNU's strcasestr()
, the naming of such functions is not standardised even when included.
One way of overcoming the lack of a standard implementation is of course to implement your own:
char* stristr( const char* str1, const char* str2 )
{
const char* p1 = str1 ;
const char* p2 = str2 ;
const char* r = *p2 == 0 ? str1 : 0 ;
while( *p1 != 0 && *p2 != 0 )
{
if( tolower( (unsigned char)*p1 ) == tolower( (unsigned char)*p2 ) )
{
if( r == 0 )
{
r = p1 ;
}
p2++ ;
}
else
{
p2 = str2 ;
if( r != 0 )
{
p1 = r + 1 ;
}
if( tolower( (unsigned char)*p1 ) == tolower( (unsigned char)*p2 ) )
{
r = p1 ;
p2++ ;
}
else
{
r = 0 ;
}
}
p1++ ;
}
return *p2 == 0 ? (char*)r : 0 ;
}
The test code below outputs:
Two Three
Two Three
NULL
cdefg
CDEFG
CdEfG
NULL
zzzz
NULL
zzzzz
NULL
int main(void)
{
char* test = stristr( "One TTwo Three", "two" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "One Two Three", "two" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "One wot Three", "two" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "abcdefg", "cde" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "ABCDEFG", "cde" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "AbCdEfG", "cde" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "1234567", "cde" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "zzzz", "zz" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "zz", "zzzzz" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "", "" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "zzzzz", "" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr( "", "zzzz" ) ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
test = stristr("AAABCDX","AABC") ;
printf( "%s\n", test == 0 ? "NULL" : test ) ;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With