Program to reverse every word in a String using methods

Опубликовано: 26 Май 2021
на канале: Aman Tripathi
31
2

INSTAGRAM |   / javac_programming  
In this Program, we first split the given string into substrings using split() method. The substrings are stored in an String array words. The program then reverse each word of the substring using a reverse for loop.

public class Example
{
public void reverseWordInMyString(String str)
{
/* The split() method of String class splits
a string in several strings based on the
delimiter passed as an argument to it
*/
String[] words = str.split(" ");
String reversedString = "";
for (int i = 0; i _ words.length; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = word.length()-1; j _= 0; j--)
{
/* The charAt() function returns the character
at the given position in a string
*/
reverseWord = reverseWord + word.charAt(j);
}
reversedString = reversedString + reverseWord + " ";
}
System.out.println(str);
System.out.println(reversedString);
}
public static void main(String[] args)
{
Example obj = new Example();
obj.reverseWordInMyString("Welcome to mymessagez world");
obj.reverseWordInMyString("This is an easy Java Program");
}
}
Output:
welcome to the mymessagez world
emoclew ot eht zegassemym dlrow
this is an easy java program
siht si na ysae avaj margorp


На этой странице сайта вы можете посмотреть видео онлайн Program to reverse every word in a String using methods длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Aman Tripathi 26 Май 2021, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 31 раз и оно понравилось 2 зрителям. Приятного просмотра!