Java Program to Print first letter of each word using regex

JavaCampus InterviewServer Side ProgrammingProgramming

In this article, we will understand how to print first letter of each word using regex. A regular expression is a sequence of characters that forms a search pattern. A regular expression can be a single character, or a more complicated pattern.

A regular expression helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.

Below is a demonstration of the same −

Suppose our input is

Input String_1: Java Program
Input String_2: Joy of learning

The desired output would be

Result_1: JP
Result_2: Jol

Algorithm

Step 1 - START
Step 2 - Declare two string values namely input_string_1 and input_string_2. Declare a regex Pattern namely string_pattern and a Matcher object namely string_matcher.
Step 3 - Define the values.
Step 4 - Using a while-loop, compute string_matcher.group() to fetch the first letter of each word.
Step 5 - Display the result
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String input_string_1 = "Java Program";
      System.out.println("\nThe first string is defined as: " +input_string_1);
      Pattern string_pattern = Pattern.compile("\b[a-zA-Z]");
      Matcher string_matcher = string_pattern.matcher(input_string_1);
      System.out.println("The first letters of the string is : ");
      while (string_matcher.find())
         System.out.print(string_matcher.group());
      System.out.println();
      String input_string_2 = "Joy of learning";
      System.out.println("\nThe second string is defined as: " +input_string_2);
      Matcher string_matcher_2 = string_pattern.matcher(input_string_2);
      System.out.println("The first letters of the string is : ");
      while (string_matcher_2.find())
         System.out.print(string_matcher_2.group());
      System.out.println();
   }
}

Output

Required packages have been imported

The first string is defined as: Java Program
The first letters of the string is :
JP

The second string is defined as: Java Program
The first letters of the string is :
Jol

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
   static void print_regex_string(String s) {
      Pattern string_pattern = Pattern.compile("\b[a-zA-Z]");
      Matcher string_matcher = string_pattern.matcher(s);
      System.out.println("The first letters of the string is : ");
      while (string_matcher.find())
         System.out.print(string_matcher.group());
      System.out.println();
   }
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String input_string_1 = "Java Program";
      System.out.println("\nThe first string is defined as: " +input_string_1);
      print_regex_string(input_string_1);
      String input_string_2 = "Joy of learning";
      System.out.println("\nThe second string is defined as: " +input_string_1);
      print_regex_string(input_string_2);
   }
}

Output

Required packages have been imported

The first string is defined as: Java Program
The first letters of the string is :
JP

The second string is defined as: Java Program
The first letters of the string is :
Jol
raja
Updated on 29-Mar-2022 11:36:28

Advertisements