Java program to count the characters in each word in a given sentence Last Updated : 31 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Write a Java program to count the characters in each word in a given sentence? Examples: Input : geeks for geeksOutput :geeks->5for->3geeks->5 Recommended: Please solve it on PRACTICE first, before moving on to the solution. Approach:Here we have to find out number of words in a sentence and the corresponding character count of each word. Here first we create an equivalent char array of given String.Now we iterate the char array using for loop. Inside for loop we declare a String with empty implementation.Whenever we found an alphabet we will perform concatenation of that alphabet with the String variable and increment the value of i.Now when i reaches to a space it will come out from the while loop and now String variable has the word which is previous of space.Now we will print the String variable with the length of the String. Implementation: JAVA class CountCharacterInEachWords { static void count(String str) { // Create an char array of given String char[] ch = str.toCharArray(); for (int i = 0; i < ch.length; i++) { // Declare an String with empty initialization String s = ""; // When the character is not space while (i < ch.length && ch[i] != ' ') { // concat with the declared String s = s + ch[i]; i++; } if (s.length() > 0) System.out.println(s + "->" + s.length()); } } public static void main(String[] args) { String str = "geeks for geeks"; count(str); } } Outputgeeks->5 for->3 geeks->5 Time Complexity: O(|S|).Auxiliary Space: O(|S|), where |S| is the length of the input string. Approach: Using split( ) and temp array Steps involved: split the words based on spaces between the sentence.Store the words in the temp array.Now traverse the temp array and then by using length() find the length of each word. Note : Sometimes split() function won't work properly when splitting should be done based on some regular expressions like OR sign (|)question mark (?)asterisk (*)plus sign (+)backslash (\)period (.)caret (^)square brackets ([ and ])dollar sign ($)ampersand (&) We need to specify them as split("\\(split char)") where (split char) would be one of the character from the above regular expression. Below is the implementation of the above approach. Java /*package whatever //do not write package name here */ import java.util.*; class GFG { public static void main(String[] args) { String s="Geeks For Geeks"; count(s); } public static void count(String s) { //split method splitting the string // based on a space String[] temp=s.split("\\ "); for(String t: temp) { // checking whether the word is not empty if(t.length()>0) System.out.println(t+" -> "+t.length()); } } } //This code is contributed by aeroabrar_31 OutputGeeks -> 5 For -> 3 Geeks -> 5 Time Complexity: O(|S|).Auxiliary Space: O(|S|), where |S| is the length of the input string. Comment More infoAdvertise with us Next Article Java program to count the characters in each word in a given sentence bishaldubey Follow Improve Article Tags : Misc Strings Java Programs DSA Java-Strings +1 More Practice Tags : Java-StringsMiscStrings Similar Reads Java program to swap first and last characters of words in a sentence Write a Java Program to Swap first and last character of words in a Sentence as mentioned in the example? Examples: Input : geeks for geeks Output :seekg rof seekg Approach:As mentioned in the example we have to replace first and last character of word and keep rest of the alphabets as it is. First 2 min read Java program to count the occurrence of each character in a string using Hashmap Given a string, the task is to write a program in Java which prints the number of occurrences of each character in a string. Examples: Input: str = "GeeksForGeeks" Output: r 1 s 2 e 4 F 1 G 2 k 2 o 1 Input: str = "Ajit" Output: A 1 t 1 i 1 j 1 An approach using frequency[] array has already been dis 2 min read Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided 2 min read Count Occurrences of a Given Character using Regex in Java Given a string and a character, the task is to make a function that counts the occurrence of the given character in the string using Regex. Examples: Input: str = "geeksforgeeks", c = 'e' Output: 4 'e' appears four times in str. Input: str = "abccdefgaa", c = 'a' Output: 3 'a' appears three times in 2 min read Java Program to Count the Occurrences of Each Character In Java, counting the occurrences of each character in a string is a fundamental operation that can be done in different ways. This process involves identifying the frequency of each character in the input string and displaying it in a readable format.Example: Input/output to count the occurrences o 5 min read Java Program to Check Whether the String Consists of Special Characters In Java, special characters refer to symbols other than letters and digits, such as @, #, !, etc. To check whether the String consists of special characters, there are multiple ways, including using the Character class, regular expressions, or simple string checks.Example:In this example, we will us 4 min read Count of words ending at the given suffix in Java Given a string str consisting of a sentence, the task is to find the count of words in the given sentence that end with the given suffix suff. Examples: Input: str = "GeeksForGeeks is a computer science portal for geeks", suff = "ks" Output: 2 "GeeksForGeeks" and "geeks" are the only words ending wi 4 min read Java Program to Find the Most Repeated Word in a Text File Map and Map.Entry interface will be used as the Map interface maps unique keys to values. A key is an object that is used to retrieve a value at a later date. The Map.Entry interface enables you to work with a map entry. Also, we will use the HashMap class to store items in "key/valueâ pairs and acc 3 min read Java program to print all duplicate characters in a string Given a string, the task is to write Java program to print all the duplicate characters with their frequency Example: Input: str = "geeksforgeeks" Output: s : 2 e : 4 g : 2 k : 2 Input: str = "java" Output: a : 2 Approach: The idea is to do hashing using HashMap. Create a hashMap of type {char, int} 2 min read Java Program to Iterate Over Characters in String Given string str of length N, the task is to traverse the string and print all the characters of the given string using java. Illustration: Input : str = âGeeksforGeeksâ Output : G e e k s f o r G e e k sInput : str = "GfG" Output : G f G Methods: Using for loops(Naive approach)Using iterators (Opti 3 min read Like