
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print First Character of Each Word in a String in Java
This article will discuss how to print the first character of each word in a given string. For example, if we have the string "Hello John", the output should be H J.
In Java, the String class is used to represent character strings. All string literals in a Java program are implemented as instances of the String class. Strings in Java are immutable, which means that once a string is created, its value cannot be changed. To print the first character of each word in a String, we have the following approaches -
First Character of Each Word Using the split() Method
In Java, the String split() method divides a string into an array of substrings based on a given delimiter. This method does not automatically print the first character of each word. It is used as part of the process to extract and print the first character of each word by splitting the string into individual words.
Syntax
Following is the syntax of the split() method in Java -
split(String regex, int limit)
Here,
- regex: Is a regular expression that specifies where to split the string. For example, for splitting by space use space (" ").
- limit (optional): It specifies maximum length of the returned array.
Example
The following program uses the split() method to divide the given string into an array of substrings. Then, it iterates through the array and uses charAt() method to print the first character of each word in the given string "Welcome to Tutorials Point" -
public class firstChar{ public static void main(String[] args) { String str = "Welcome to Tutorials Point"; System.out.println("The given string is: " + str); //using split() method String split_str[] = str.split(" "); System.out.print("First character of each word is: "); //iterate through each word for(int i = 0; i<split_str.length; i++){ //used charAt() method to print single char System.out.print(split_str[i].charAt(0) +" "); } } }
The above program prints the first character of each word -
The given string is: Welcome to Tutorials Point First character of each word is: W t T P
Using the toCharArray() Method
Here, we have another way to print the first character of each word in a string, using the String toCharArray() method. This method converts the entire string into an array of characters.
Syntax
Following is the syntax of the toCharArray() method in Java:
public char[] toCharArray()
Example
In the example below, we use the toCharArray() method to convert the entire string "Hello World" into an array of characters. Then, we iterate through the array and print each character, followed by a space:
public class firstChar { public static void main(String[] args) { String str = "Hello World"; System.out.println("The given string is: " + str); char c[] = str.toCharArray(); System.out.print("The first character of each word: "); for (int i=0; i < c.length; i++) { // Logic to implement first character of each word in a string if(c[i] != ' ' && (i == 0 || c[i-1] == ' ')) { System.out.print(c[i] + " "); } } } }
Output
Following is the output of the above program ?
The given string is: Hello World The first character of each word: H W