The strstr of C language (function) learning Strcasestr

Source: Internet
Author: User

First, the STRSTR function uses

[1] function prototypes

Char *strstr (const char *haystack, const char *needle);

[2] header file

#include <string.h>

[3] function function

Search for the first occurrence of "substring" in "specified string"

[4] parameter description

Haystack        --the target string that is being looked for "parent string" needle          --The string object "substring" to find

Note: If needle is null, the " parent string " is returned

[5] return value

(1) Successfully found, returns the char * pointer (2) where the first occurrence in the parent string is not found, that is, there is no such substring, returns: "NULL"

[6] examples of programs

#include <stdio.h> #include <string.h>int main (int argc, char *argv[]) {    char *res = strstr ("Xxxhost: Www.baidu.com "," host ");    if (res = = NULL) printf ("Res1 is null!\n");    else printf ("%s\n", res);    Print:--> ' host:www.baidu.com '    res = strstr ("xxxhost:www.baidu.com", "Cookie");    if (res = = NULL) printf ("Res2 is null!\n");    else printf ("%s\n", res);    Print:--> ' Res2 is null! '    return 0;}

[7] special Instructions

Note: The parameters in the STRSTR function are strictly " case-sensitive "

Second, strcasestr function

[1] description

The function and use method of STRCASESTR function is basically consistent with strstr.

[2] differences

The STRCASESTR function is "case insensitive" when comparing "substring" to "parent string"

[3] function prototypes

#define _GNU_SOURCE#INCLUDE <string.h>char *strcasestr (const char *haystack, const char *needle);

[4] examples of programs

#define _GNU_SOURCE             //macro definition must have, otherwise the compilation will have warning warning message # include <stdio.h> #include <string.h>int main (int ARGC, Char *argv[]) {    char *res = strstr ("xxxhost:www.baidu.com", "Host");    if (res = = NULL) printf ("Res1 is null!\n");    else printf ("%s\n", res);     Print:--> ' host:www.baidu.com '    return 0;

[5] Important details

If you do not define a "_gnu_source" macro when programming, there will be a warning message when compiling

Warning:initialization makes pointer from integer without a cast

Reason:

The STRCASESTR function is not a standard C library function, it is an extension function. The function returns the int type without declaring the default before the call

Solve:

To add #define _gnu_source before all header files include  

Another workaround: (but not recommended)

Under Define header file, manually add the prototype declaration of the STRCASESTR function yourself

#include <stdio.h>, .... extern char *strcasestr (const char *, const char *);         This method can also eliminate compile-time warning messages

The strstr of C language (function) learning Strcasestr

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.