C++ Program to Print first letter of each word using regex


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 utilized as general-purpose search patterns. For instance, by constructing a straightforward string known as a regular expression, we may implement password validation logic with at least one capital, one lowercase, one number, one special character, and a total length of 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 a specified string. Here, we'll look at a sentence that uses space to divide the words. Regardless of whether a character is in uppercase or lowercase, the computer will read the string, split it using regex, and return the first character of each word.

To use regular expressions, we need to import the regex library using the ‘regex’ header. To use a regular expression, we need the following syntax −

Syntax (Creating regex object)

regex obj_name( <regular expression> )

After defining regex, we can use them in multiple ways. We will see our intended approach in the following. Now to read the first characters from words, the regex syntax will be like the below −

Syntax (To read first character of words)

\b[a-zA-Z]

Here ‘\b’ represents the beginning of words. The [a-zA-Z] represents uppercase or lowercase letters which are in the range of ‘a’ to ‘z’ or ‘A’ to ‘Z’. And only one of them is taken. Now let us see the iterator object which is being used to read all selected matches −

Syntax (regex iterator)

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 beginning and the ending pointers of the 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 return those elements which are coming from the match (where it is matched), when the submatch is -1, it represents where the matching is not done (reverse of the submatch 0 mode).

Algorithm

  • Take the string s as input
  • define regular expression with '\b[a-zA-Z]'
  • perform matching on s with the expression
  • define an iterator object to read only those matches
  • for each item in iterator object, do
    • display the item
  • end for

Example

#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;
}

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, 

Conclusion

Regular expressions are utilised in strings to match common patterns. The regular expression library (regex) is available in all high level languages, including Java, Python, Javascript, Dart, and C++. There are numerous applications for it. A regular expression that reads the first character of each word has been defined in this article. We need an iterator object, which reads each matching character individually, to cycle through them.

Updated on: 14-Dec-2022

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements