The basics of programming in c++ for beginners

Search substring in string

Now we will look at examples, search algorithm might look for a substring within a string. Examples are based on the standard library functions, it is in these functions, and have all the amenities of writing programs. But the classic parsing algorithm, based on loops and comparisons, It is also quite remarkable. Therefore, we will consider it in the same lesson.

The algorithm itself is very simple in principle,. There are two strings. For example "Hello world"  and "the"

Work will be in two loops:

  1. The first will be to carry out a passage across the string, and look for location first letter search string ( "the" ).
  2. Second, since the position of the first letter found – check, which letters are after it, and how many of them match in a row.

We illustrate searching for a substring within a string:

Search algorithms ++, search for a substring in a string, c++, how to find a substring in a string, C ++ programming for beginners, report, course work

The first two iterations of the loop compared the letters would not be the same (marked in red). The third iteration of the desired letter (the first character of the desired word) It coincides with the character in the string, where the search. In this match the work included the second loop.

It is designed to count the number of characters after the first in the search string, which will coincide with the characters in the original string. If one of the symbols do not match – Loop exits. It makes no sense to chase the loop of wasted, after the first discrepancy, since it is already clear, that there is no title.

In the third iteration matched only the first character of the string, but the second is not the same. The first loop will have to continue. The fourth iteration gives the required results – match all the characters of the string with a portion of the source string. And since all the characters matched – substring found. Algorithm work can be completed.

We will see, It looks like a classic substring search code string in C ++:

Two loops performed each his task. One stomping on the string hoping to find “head” search word (first character). The second finds, whether there is found after “head” “body” sought. And checks, whether it does not lie “body” at the end of the string. That is. is not found if the word length is one more than the required length of the string, considering, Null terminator that falls into this unit ( '' ).

Search algorithms ++, search for a substring in a string, c++, how to find a substring in a string, C ++ programming for beginners, report, course work

We see, what program found begin substringhowever, in the cells of the character array index 0 and 4. But why? After all, in a wordParapapasuch substring. The thing '' .

In general, the meaning of the algorithm ends here. No more difficulties than zero at the end of the line there. However, should pay attention to the multiplicity of search. What, if we need to find the string in several positions?

How many times the search term occurs in the string and in what places? It is designed to control and the third parameter – int n – number of occurrences of a string. If you put the unit back – He finds the first match title. If a deuce, it will make the first loop to skip first found, and seek a second. If three – look for the third and so on. Each search term found, occurrences of this counter is decremented. This allows us to describe the search loop:

That is, to find the first, second, third, fourth match… As long as the function does not return -1, that indicate the absence of N-tion of the title in a string.

Now, for comparison, search for a substring in a string ++ hederom string.

It is all! Loop string C ++ is provided by find(), returns the number of the cell, which begins with the body of the search string in the source string. Result:

Search algorithms ++, search for a substring in a string, c++, how to find a substring in a string, C ++ programming for beginners, report, course work

How the multiplicity? Yes please:

Function find() takes second parameter symbol number, from which to start search. That is. When you find the first occurrence of, its value is incremented and find() search continues with the next character after the head found. Result:

Search algorithms ++, search for a substring in a string, c++, how to find a substring in a string, C ++ programming for beginners, report, course work

All this is in C ++, himself class string comfortable enough to work with strings is, as strings, rather than just an array of characters.

10 thoughts on “Search substring in string

  1. I think it is better, because it saves testing zero symbol

    1. Actually, in C, and inherited in C ++, considered, that the numerical value 0 false matches (violation of conditions), and any non-zero value – true (compliance with conditions).
      Therefore, if formally( n – 1 ) equivalent if( n != 1 ).

      P.S. Formally, because, that the code is correct I do not even delved.

    2. For this record, you must get used to, because such a record is not uncommon in real code in the C / C ++, especially when compared указателей with a NULL value:

      int *ptr = ...
      ...
      if( ptr ) { ... }

    1. Abhilash (Head AISPNTRF - Association of the Indian Community of Programmers in the Russian Federation ) says:

      Alexander, Hello! Thank you for your comment, he brought the programming community closer to, to observe all the subtleties and nuances of a competent and understandable construction of program code in the beautiful C / C ++ language, which rules out the fact, that man, so deeply interested in information technology, will have the misfortune to be confused and 2 days parse enumeration through char*. We sincerely thank you and wish you an exceptionally understandable code in the vastness of the global network.

Leave a Reply

Your email address will not be published. Required fields are marked *