Home > Article > Backend Development > 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 -
regex obj_name( <regular expression> )
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:
\b[a-zA-Z]
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 -
regex_token_iterator<string::iterator> iterator_name( <begin pointer of string>, <ending pointer of string>, <regular expression>, <submatch>);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)
#include <iostream> #include <regex> using namespace std; string solve( string s){ string ret = ""; regex e("\b[a-zA-Z]"); regex_token_iterator<string::iterator> i(s.begin(), s.end(), e, 0); regex_token_iterator<string::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; }
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,
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!