Reverse Each Word
public class ReverseEachWord {
public static void main(String[] args) {
String sentence = "I love Java programming";
String[] words = sentence.split(" ");
String reversedSentence = "";
for (String word : words) {
String reversedWord = new StringBuilder(word).reverse().toString();
reversedSentence += reversedWord + " ";
}
System.out.println("Reversed Each Word: " + reversedSentence.trim());
}
}
Logic
Split the sentence into words using:
sentence.split(" ");
Reverse each individual word using StringBuilder.reverse() while keeping the original word order unchanged.
Example Output:
I evol avaJ gnimmargorp
Reverse the Word Order
String[] words = sentence.split(" ");
String reversedSentence = "";
for (int i = words.length - 1; i >= 0; i--) {
reversedSentence += words[i] + " ";
}
Logic
Traverse the array of words from the last element to the first.
The position of each word changes, while the characters inside each word remain unchanged.
Example:
Input : I love Java
Output: Java love I
Longest & Shortest Word
public class LongestWord {
public static void main(String[] args) {
String sentence = "I love Java programming language";
String[] words = sentence.split(" ");
String longestWord = "";
int maxLength = 0;
for (String word : words) {
if (word.length() > maxLength) {
maxLength = word.length();
longestWord = word;
}
}
System.out.println("Longest Word: " + longestWord);
}
}
Logic
- Split the sentence into words.
- Compare the length of every word.
- Store the longest word encountered during traversal.
For the shortest word, use the same logic while tracking the minimum length.
Count Words & Word Frequency
Count Words
Count the number of words using:
sentence.trim().split("\\s+").length
The regular expression \\s+ handles one or more whitespace characters.
Word Frequency
public class WordFrequency {
public static void main(String[] args) {
String sentence = "Java is easy and Java is powerful";
String[] words = sentence.split(" ");
Map<String, Integer> wordCount = new HashMap<>();
for (String word : words) {
word = word.toLowerCase();
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}
for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}
Logic
- Convert every word to lowercase.
- Store the frequency of each word in a
HashMap. - Display every word along with its count.
Check Anagram
Two strings are anagrams if they contain exactly the same characters in a different order.
Approach 1 – Sort & Compare
- Convert both strings into character arrays.
- Sort both arrays using
Arrays.sort(). - Compare them using
Arrays.equals().
Approach 2 – Character Frequency
- Count the characters in the first string.
- Subtract the counts using the second string.
- If every frequency becomes zero, both strings are anagrams.
Normalize the strings by converting them to lowercase and removing spaces before comparison.
Sort Words Alphabetically
public class SortWordsAlphabetically {
public static void main(String[] args) {
String sentence = "Java is a powerful programming language";
String[] words = sentence.toLowerCase().trim().split("\\s+");
Arrays.sort(words);
for (String word : words) {
System.out.println(word);
}
}
}
Logic
- Convert the sentence to lowercase.
- Split it into words.
- Sort the words alphabetically using
Arrays.sort(). - Print each word.
Character Frequency
Use a HashMap to count the occurrence of every character.
map.put(ch, map.getOrDefault(ch, 0) + 1);
If insertion order needs to be preserved, use a LinkedHashMap.
Frequently Asked Questions
What is the difference between reversing each word and reversing word order?
- Reverse Each Word: The position of the words remains the same, but each individual word is reversed.
- Reverse Word Order: The order of the words changes, while each word itself remains unchanged.
How do you find the longest word in a sentence?
Split the sentence into words and compare the length of every word while traversing the array.
How do you count word frequency?
Convert every word to lowercase and store the count in a HashMap using:
map.put(word, map.getOrDefault(word, 0) + 1);
How do you check whether two strings are anagrams?
Normalize both strings and then either:
- Sort and compare the character arrays.
- Compare character frequencies.
How do you sort words alphabetically?
Split the sentence using:
split("\\s+")
and sort the resulting array using:
Arrays.sort(words);