string::find
Find content in string
Searches the string for the first occurrence of the sequence specified by its arguments.When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.
- str
- Another string with the subject to search for.
- pos
- Position of the first character in the string to be considered in the search.
If this is greater than the string length, the function never finds matches.
Note: The first character is denoted by a value of 0 (not 1): A value of 0 means that the entire string is searched. - s
- Pointer to an array of characters.
If argument n is specified (3), the sequence to match are the first n characters in the array.
Otherwise (2), a null-terminated sequence is expected: the length of the sequence to match is determined by the first occurrence of a null character. - n
- Length of sequence of characters to match.
- c
- Individual character to be searched for.
size_t is an unsigned integral type.
size_t:
It is a type able to represent the size of any object in bytes:size_tis the type returned by thesizeof operator and is widely used in the standard library to represent sizes and counts.Example:
#include <iostream>
#include <string>
#include<conio.h>
using namespace std;
int main ()
{
string second;
string first ("Sir Asim is a good programmer in the world....");
cout << first<< endl;
cout << endl << endl;
cout << "\t\tenter word search from the string......:: ";
cin >> second;
size_t found=first.find(second);
if(found!=string::npos)
std::cout << "first " << second <<"found at: " << found << '\n';
getch();
return 0;
}
...................................................................................................................................................
#include <iostream>
#include <string>
#include<conio.h>
using namespace std;
int main ()
{
string second;
string first ("Sir Asim is a good programmer in the world....");
cout << first<< endl;
cout << endl << endl;
cout << "\t\tenter word search from the string......:: ";
cin >> second;
size_t found=first.find(second);
if(found!=string::npos)
{
cout << "word is found" << found << endl;
string repl;
cout << "enter word you wand to replace with: "<< second << ": ";
cin >> repl;
first.replace(first.find(second),second.length(),repl);
cout << first<< endl;
}
else
cout << "sorry word not found: " << endl;
getch();
return 0;
}