English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Example Code for String String Search and Match

When writing C++In the program, we will always encounter the situation of finding a substring from a string. In C, we often use strstr() or strchr() for this purpose. And for C++In the string of C++, we often use find().

C++:#inlcude<string>
C: #include<string.h>

find():Search for a specified single character or character array in a string. If found, return the starting position of the first match; if not found, return string::npos.

find_first_of():The search is performed in a target string, and the return value is the position of the first character that matches any character in the specified character group. If no matching content is found, then return npos.

find_last_of():Search within a target string and return the position of the last character that matches any character in the specified character group. If no matching content is found, return npos.

find_first_not_of():Search within a target string and return the position of the first element that does not match any character in the specified character group. If no such element is found, return npos.

find_last_not_of():Search within a target string and return the position of the largest element that does not match any character in the specified character group. If no such element is found, return npos.

rfind(): Search for a specified single character or character group from the end of a string. If found, return the starting position of the first match; if not found, return npos.

find(string, int):The first parameter is used to indicate the character to be searched, and the second parameter indicates where to start searching the substring in the string (the default search position is 0).

Example: String Matching:

#include "stdafx.h"
#include<iostream>
#include<math.h>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string T;//Original string
string P;//Pattern
while(cin>>T>>P)
{ 
int count=0;
int begin=-1;
while((begin=T.find(P,begin+1)=string::npos)
{
count++;
}
cout<<count<<endl;
}
int z;
cin>>z;
return 0;
}

That's all the C++ All the example code for string string search and match are included, everyone please support and cheer for the tutorial!

You May Also Like