Write a Java program to count the characters in each word in a given sentence?
Input : geeks for geeks Output : geeks->5 for->3 geeks->5
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 concatination 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.
geeks->5 for->3 geeks->5
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Java program to swap first and last characters of words in a sentence
- Program to replace a word with asterisks in a sentence
- Program for length of the longest word in a sentence
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Check if a word is present in a sentence
- Sums of ASCII values of each word in a sentence
- Print longest palindrome word in a sentence
- Print each word in a sentence with their corresponding average of ASCII values
- C program to find and replace a word in a File by another given word
- C program to Replace a word in a text by another given word
- Program to count vowels, consonant, digits and special characters in string.
- Next word that does not contain a palindrome and has characters from first k
- Possibility of a word from a given set of characters
- Print list items containing all characters of a given word
- Arrangement of the characters of a word such that all vowels are at odd places
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.