This Java program reverses each word in a given string by splitting the string into individual words, reversing each word, and then concatenating the reversed words back into a new string. Here's a breakdown of how the program works:
The reverseWithStringConcat() method takes a single string argument and returns the reversed string. It uses a for loop to iterate over the characters of the input string in reverse order, and concatenates them to an empty string rev_word using the + operator. The reversed string is returned by the method. The display() method takes two string arguments and prints them to the console.
class Reverse_EachWord { public static void main(String[] args) { reverseEachWord("String and String function"); } private static String reverseEachWord(String str) { String[] words = str.split(" "); String rev = ""; for (String word: words) { rev = rev + reverseWithStringConcat(word) + " "; } display(str, rev); return rev; } private static final String reverseWithStringConcat(String string) { String rev_word = ""; for (int i = (string.length() - 1); i >= 0; i--) { rev_word = rev_word + string.charAt(i); } return rev_word; } private static final void display(String str, String rev) { System.out.println(str); System.out.println(rev); } }
String and String function gnirtS dna gnirtS noitcnuf
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions