String Manipulation Functions
String Manipulation Functions
To count the frequency of each word in a string, `public static void wordFrequencyCount(String s1)` should split the string into words using a delimiter like space, store each word in a map, and then iterate over the array incrementing the count for each word encountered in the map .
To arrange characters in lexicographical order, `static void sortedOrder(String s)` can be implemented. This involves converting the string into a character array, applying a sorting algorithm such as bubble sort or merge sort, and reconstructing the sorted string from the character array .
The method `static String changeCase(String s)` converts each character to its opposite case by iterating over the string, checking if it's uppercase or lowercase, and changing appropriately to toggle the case for each character without using built-in case functions .
To find the largest palindrome in a string, iterate through all possible substrings, check each substring using a method similar to `palindrome(String s)`, and update the record for the longest one found during these checks .
The method `static String allTrim(String s)` can be used to trim spaces from both ends of a string. It requires creating custom functions such as `ITrim` to remove leading spaces and `rTrim` for trailing spaces. Combining these functions will provide a complete trimming effect equivalent to `trim()` .
To determine if a string is a palindrome without using built-in functions, a method `static boolean palindrome(String s)` can be used by comparing characters from the start and the end of the string moving towards the center. If all corresponding characters match, the string is a palindrome, otherwise, it is not .
To calculate the character count of a string without including spaces, one can implement a method such as `static int charCount(String s)` which iterates through each character in the string and increments a counter only if the character is not a space. This approach ensures spaces are excluded from the count .
For replacing a substring within a string without using built-in index functions, `static String replace(String s1, String s2, String s3)` involves manually searching for the substring using a loop, checking for matching starting positions, and performing the replacement by constructing the new string with the modified content .
For reversing each word individually in a string, use the method `public void reverseEachWord(String s)`, which splits the string into words, reverses each word independently, and joins them back together with spaces separating them. This word-by-word manipulation ensures each word is reversed while maintaining their order .
To check if two strings are anagrams, `compare(String s1, String s2)` sorts both strings and compares the sorted results. Anagrams will match exactly in their sorted forms. Alternatively, character count comparisons can be used to verify each character's frequency in both strings .