C++ program using regular expressions to print the first letter of each word

王林
Release: 2023-08-26 21:33:17
forward
1098 people have browsed it

C++ program using regular expressions to print the first letter of each word

A useful tool for string operations is regex. This may be found in virtually all high-level Current programming languages, including C. Regular expressions (Regex) are used as Universal search pattern. For example, by building a simple string Known as regular expressions, we can implement password verification logic using at least One uppercase letter, one lowercase letter, one number, one special character, and the total length is at least 8 characters.

In this tutorial, we'll look at how to use C to display only the first letters of words included within the specified string. Here we will look at a sentence that uses spaces to separate words Whether the characters are uppercase or lowercase, the computer will read them Split the string using a regular expression and return the first character of each word.

To use regular expressions, we need to import the regex library using the ‘regex’ header. To Using regular expressions we need the following syntax -

Syntax (create regular expression object)

regex obj_name(  )
Copy after login

After defining regex, we can use them in multiple ways. We will see our intended approach under. Now to read the first character from the word, the syntax of the regular expression will be like this The following content is:

Syntax (read the first character of the word)

\b[a-zA-Z]
Copy after login

Here, ‘\b’ represents the beginning of the word. [a-zA-Z] represents uppercase or lowercase letters lowercase letters which are in the range of ‘a’ to ‘z’ or ‘A’ to ‘Z’. And only one of them is taken. Now let's take a look at the iterator object being used to read all selected matches -

Syntax (regular expression iterator)

regex_token_iterator iterator_name( , , , );
Copy after login
In this iterator, the first two parameters are the start and end pointers. string object. The third parameter is the given regular expression object which we have Created before. The fourth parameter is the submatch. When the submatch is 0, it will Returns the content from the matched element (at the time of the match), when submatch is -1, it represents where the matching is not done (reverse of the submatch 0). submatch is -1, indicating that the match is not completed (opposite of submatch 0)

algorithm

  • Take string s as input
  • define regular expression with '\b[a-zA-Z]'
  • Use expressions to match s
  • Define an iterator object to read only matches
  • for each item in iterator object, do
    • Show items
  • End loop

Example

#include  #include  using namespace std; string solve( string s){ string ret = ""; regex e("\b[a-zA-Z]"); regex_token_iterator i(s.begin(), s.end(), e, 0); regex_token_iterator end; while (i != end) { ret += (*i++); ret += ", "; } return ret; } int main(){ string s = "A string to read only the first letter of words"; cout << "Given String: " << s << endl; cout << "The first letter of each word: " << solve( s ) << endl; s = "Central Pollution Control Board"; cout << "Given String: " << s << endl; cout << "The first letter of each word: " << solve( s ) << endl; }
Copy after login

Output

Given String: A string to read only the first letter of words The first letter of each word: A, s, t, r, o, t, f, l, o, w, Given String: Central Pollution Control Board The first letter of each word: C, P, C, B,
Copy after login

Conclusion

Expressions used in strings to match common patterns Expression libraries (regular expressions) are available in all high-level languages including Java, Python Javascript, Dart and C. It has many applications. a regular expression, reads the first character of each word has been defined in this article. We need an iterator , which reads each matching character one by one in order to loop over them.

The above is the detailed content of C++ program using regular expressions to print the first letter of each word. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!