Java String Manipulation Examples
Java String Manipulation Examples
The character frequency analysis in Java involves iterating over each letter of the alphabet. For each letter, a nested loop is used to count occurrences in the input string. The results, showing each character and its count, are printed only if the count is greater than zero. This approach ensures the frequency count of each character in the input string .
The method to capitalize the first letter of each word in a string involves using a loop to iterate through each character of the string. The logic is to check if a character is a space followed by a non-space character, then capitalize the non-space character. This is done by concatenating the character with its uppercase version using `Character.toUpperCase()` .
The technique to remove all whitespace from a Java string utilizes the `replaceAll()` method with a regex pattern matching spaces. Specifically, `str.replaceAll(" ", "")` removes all space characters from the string, resulting in a continuous sequence of non-space characters left in the resulting string .
Generating non-empty substrings in Java involves using two nested loops. The outer loop starts from the beginning and goes to the end of the string, while the inner loop begins from the next character of the outer index and goes to the length of the string plus one. This nested iteration allows all possible substrings to be printed by capturing characters between index ranges determined by the loops .
To reverse each word in a sentence using Java, you can split the sentence into words, reverse each word individually, and then print the reversed words with spaces. Specifically, this involves using `String.split()` to obtain each word, then using `StringBuilder.reverse()` to reverse each word, and finally concatenating the reversed words to form the output .
To check if a string is a palindrome in Java, you can reverse the string and compare it with the original. This involves taking the input string, converting it to a character array, reversing the character array, and then checking if the reversed string matches the original. If they are equal, the string is a palindrome; otherwise, it is not .
To perform an n-position rotation of a string in Java, concatenate the string with itself, and then extract a substring starting at the n-th position with a length equal to the original string. This leverages string duplication and substring extraction to simulate the rotation effect .
The Java program toggles the case of each word by splitting the input string into words, then transforming each word such that the first letter is lowercase and the rest of the letters are uppercase. This is accomplished using `substring()` for string manipulation and `toLowerCase()` and `toUpperCase()` methods to adjust the cases of the first and rest of the letters, respectively .
To determine if two strings are anagrams in Java, the strategy involves converting both strings to character arrays, sorting these arrays, and then comparing them. If the sorted arrays are identical, the strings are anagrams. This approach ensures that the characters and their frequencies match in both strings .
Character toggling at even and odd indexes involves iterating over the string, checking the index of each character, and applying different case transformations based on its parity. Even index characters are converted to uppercase, while odd index characters are converted to lowercase, using methods `toUpperCase()` and `toLowerCase()`, respectively, allowing systematic toggling throughout the string .