Sunday, May 19, 2024
HomejQuery5 Examples to Study C++ String Discover Perform

5 Examples to Study C++ String Discover Perform


An introduction to C++ String Discover Perform

  • The string discover() operate in C++ is used to discover a substring within the given string.
  • The discover() operate belongs to the C++ string class.
  • We should embrace <string> within the header part (see the applications under)
  • For instance, we’ve the string “That is C++ tutorial”.
  • We need to discover if the above string accommodates the phrase “tutorial” or not.
  • Within the discover() operate of C++, we will specify that string and supply the phrase “tutorial”.
  • The discover technique returns the place of the phrase if discovered.
  • If the place of the given phrase/character is discovered – which means the phrase exists – in any other case it’s not.
  • If the searched string isn’t discovered the discover technique return string::npos. npos is the static member fixed worth with the best worth for a component of size_t.

Syntax of utilizing C++ discover() operate

Following is the syntax for utilizing the discover() operate:

str.discover(seq, place, rely)

Following are the parameters description of discover operate:

  • Str: is the string object. The string you need to search in.
  • Seq: It’s the sequence of characters that you just need to search within the str.
  • Pos: place or index within the str to begin the search. The default is 0. It’s optionally available.
  • Rely: Variety of characters that should match within the str. The default is the size of the seq. That is additionally an optionally available parameter.

Now allow us to take a look at just a few examples of utilizing the string discover operate with a number of parameters.

An instance of C++ discover with a single parameter

On this instance, we solely used the required parameter within the discover() operate. Now we have created a string (str1) and used it with the discover technique.

One other string variable is said (str2) which is used because the sequence to look within the discover technique. See the code and output:



Output:

CPP-string-find

What if a given sequence happens twice or extra?

On this instance, we looked for a personality ‘s’ within the given string. It happens twice within the sentence. See what it outputs:



Output:

CPP-find-twice

An instance with sequence and place parameter

In this system under, we used two parameters within the discover operate. The primary is the sequence (similar to the above instance i.e. str2) and the second is the place parameter. First, take a look on the code and output under:



First output as we set place 3:

Once we set place 8:



CPP-string-find-pos-2

  • As sequence/phrase “good” begins at place 7. Within the first program, we see the output as 7.
  • Nevertheless, within the second program, we set the looking place to begin at 8.
  • Because the discover operate was unable to search out the phrase “good” from place 8, so it returned string::npos. Which is described within the first part.

What if the sequence happens twice?

We are going to seek for the letter ‘o’ on this instance. It happens twice within the given sentence. Nevertheless, the place is given so the primary incidence must be bypassed.

Allow us to see the output:



Output:

CPP-find-twice-pos

You noticed, although the primary incidence is at place 5, it didn’t return. The reason being we set place 6. So it returned the following incidence of ‘s’ which is 15.

Utilizing all three parameters instance

Within the following program, we additionally used a 3rd optionally available parameter i.e. rely. See the code and output under:



Output:

CPP-string-find-count

`

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments