C ++ strstr () function and a string, <string.h>, <cstring> distinction

About strstr () function

In the library file cstring, rather than the string, the usage is:

str (str1, str2);

str1: is to find the target string expr; ession to search.

str2: To find the object The string expression to find.

Note str1 and str2 must be char *;

Returns: If str2 is a substring of str1, str2 address at the first occurrence of str1 is returned; if the substring of str1 str2 not, NULL is returned.

By definition a bit shy understand. For example to know.
For example:
char str2 = "CDEF";
char str1 = "ABCDEFGH";
through the function returns
strstr (str1, str2) = cdefgh ;

If str1 does not contain str2.
str2 = char "cxef";
char str1 = "ABCDEFGH";
through the function returns
strstr (str1, str2) = NULL ;


 

About string and cstring, string.h difference:

<string.h>
<string.h> is a version of the C header file, containing such strcpy, strcat string functions like.

<string>
<string> is defined in the standard C ++ header file, which defines a string class string, which contains the string class various operations, such as s.size (), s.erase (), s.insert ()Wait. But <string> C also contains the old version of the string operations such as strcpy, strcat the like, which is equivalent, in <string> In addition to defining its own file string class, but also added a #include <string .h> C contains a version string operations.


<cstring>
in C ++ standardization (1998) process for the previous compatibility, standardization organizations all of these documents have carried out a new definition is added to the standard library, after adding the file name to add a "c" prefix and removing the suffix .h, it became cstring string.h header file header. But its implementation is identical or compatible with the previous, this is <cstring> source, I do not think one more thing. It is equivalent to the standard library organization to cover up a chapter, he said, "You are my part of the standard library."


 

#include <iostream>
#include <cstring> //必须是cstring 
using namespace std;
const int N=100;
void mystrlwr(char *ps)
{
    while(*ps)
    {
        if('A'<=*ps && *ps<='Z')
            *ps+=32;
        ps++;
    }
}
int main()
{
    char key[N+1],s[N+1],lowerkey[N+1],lowers[N+1];
    int option,n;
    cin>>key>>option>>n;
    strcpy(lowerkey,key);
    mystrlwr(lowerkey);
    for(int i=0;i<=n;i++)
    {
        cin>>s;
        if(option==0)
        {
            strcpy(lowers,s);
            mystrlwr(lowers);
            if(strstr(lowers,lowerkey))
            cout<<s<<endl;
        }
        else{
            if(strstr(s,key))
            cout<<s<<endl;
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/awangkuo/p/12524763.html