I see a few problems all in your first method:
1. In your first method you are returning words, which is just an array split, that's why you are getting the original output, because that's what you are returning. After your split, you can instantiate a new String to hold the reversed words with a =+. After each call to the second reversedWord method, you can add to the string an empty space " ". However this will also add a space after the last word of the sentence, so to make it even better I suggest you check if you are working on the last word, if not you add the space, if so, then you don't.
Your second method is fine.
Here is the working code:
```java
public class Solution {
public static void main(String[] args) {
System.out.println(reverseWords("My name is Pedro"));
}
public static String reverseWords(String s) {
String[] words = s.split(" ");
String ss = "";
for(String word : words){
ss += reverseWord(word);
ss += " ";
}
return String.join(" ", ss);
}
public static String reverseWord(String s){
char[] letters = s.toCharArray();
s="";
for(int i=letters.length-1;i>=0;i--){
s=s + letters[i];
}
return s;
}
}
```
I also suggest you rename your methods better to signify what they are really doing. My variably ss is also very badly named. Answer from Pedro Cabral on teamtreehouse.com
w3resource
w3resource.com › java-exercises › string › java-string-exercise-45.php
Java - Reverse words in a given string
StringBuilder reverse = new StringBuilder(); // Iterate through each word, reverse it, and append it to the result string. for (String tmp: words) { sb = new StringBuilder(tmp); reverse.append(sb.reverse() + " "); } // Remove the trailing space ...
GeeksforGeeks
geeksforgeeks.org › dsa › reverse-words-in-a-given-string
Reverse words in a string
using System; class GFG{ public static string reverseWords(string s){ // Convert the string to a char array // for in-place operations char[] chars = s.ToCharArray(); int n = chars.Length; // Reverse the entire string reverse(chars, 0, n); int i = 0; for (int l = 0; l < n; ++l){ if (chars[l] != '.'){ // Add a dot between words if needed if (i != 0) chars[i++] = '.'; // Find the end of the word int r = l; while (r < n && chars[r] != '.') chars[i++] = chars[r++]; // Reverse the current word reverse(chars, i - (r - l), i); // Move to next word l = r; } } return new string(chars, 0, i); } // Utility to reverse part of the char array private static void reverse(char[] arr, int left, int right){ right--; while (left < right){ char temp = arr[left]; arr[left++] = arr[right]; arr[right--] = temp; } } static void Main(){ string s = "..geeks..for.geeks."; Console.WriteLine(reverseWords(s)); } }
Published May 3, 2010
W3Resource
w3resource.com › csharp-exercises › basic › csharp-basic-exercise-28.php
C# - Reverse the words of a sentence
using System; using System.Collections.Generic; // This is the beginning of the Exercise28 class public class Exercise28 { // This is the main method where the program execution starts public static void Main() { string line = "Display the pattern like pyramid using the alphabet."; Console.WriteLine("\nOriginal String: " + line); // Displaying the original string string result = ""; // Initializing an empty string to store the reversed words List<string> wordsList = new List<string>(); // Creating a list to store reversed strings string[] words = line.Split(new[] { " " }, StringSplitOptions.No
Top answer 1 of 2
1
I see a few problems all in your first method:
1. In your first method you are returning words, which is just an array split, that's why you are getting the original output, because that's what you are returning. After your split, you can instantiate a new String to hold the reversed words with a =+. After each call to the second reversedWord method, you can add to the string an empty space " ". However this will also add a space after the last word of the sentence, so to make it even better I suggest you check if you are working on the last word, if not you add the space, if so, then you don't.
Your second method is fine.
Here is the working code:
```java
public class Solution {
public static void main(String[] args) {
System.out.println(reverseWords("My name is Pedro"));
}
public static String reverseWords(String s) {
String[] words = s.split(" ");
String ss = "";
for(String word : words){
ss += reverseWord(word);
ss += " ";
}
return String.join(" ", ss);
}
public static String reverseWord(String s){
char[] letters = s.toCharArray();
s="";
for(int i=letters.length-1;i>=0;i--){
s=s + letters[i];
}
return s;
}
}
```
I also suggest you rename your methods better to signify what they are really doing. My variably ss is also very badly named.
2 of 2
0
Thank you, Pedro ! It worked !
takeuforward
takeuforward.org › data-structure › reverse-words-in-a-string
Reverse Words in a String - Tutorial
Search for a command to run
Utilitytools
utilitytools.net › text › reverse
Reverse Text Tool – Flip & Reverse Words Online
Character-by-Character Reversal: Reverses the entire text one character at a time. Example: “Hello World” → “dlroW olleH”. Word Order Reversal: Reverses the order of words but keeps the letters in each word unchanged.
Techcrashcourse
techcrashcourse.com › 2017 › 09 › java-program-to-reverse-words-of-sentence.html
Java Program to Reverse Words of a Sentence
For Example, Input Sentence : I love Java Programming Output Sentence : Programming Java love I To split a string to multiple words separated by spaces, we will call split() method. ... split() method returns an array of Strings, after splitting string based of given regex(delimiters). package ...
w3resource
w3resource.com › java-exercises › string › java-string-exercise-46.php
Java - Reverse every word in a string using methods
String[] each_words = str1.split(" "); String revString = ""; // Iterate through each word in the array. for (int i = 0; i < each_words.length; i++) { String word = each_words[i]; String reverseWord = ""; // Reverse each word character by character. ...
w3resource
w3resource.com › python-exercises › python-conditional-exercise-5.php
Python Exercise: Reverse a word - w3resource
# Prompt the user to input a word word = input("Input a word to reverse: ") # Iterate through the characters of the word in reverse order for char in range(len(word) - 1, -1, -1): # Print each character from the word in reverse order without a new line (end="") print(word[char], end="") # Print a new line to separate the reversed word from the next output print("\n") ... Write a Python program to reverse a word entered by the user using slicing.
Django Central
djangocentral.com › reverse-a-sentence
Python Program To Reverse a Sentence
Join the list in the reverse order which ultimately is the reversed sentence. sentence = "dread it run from it destiny still arrives" word_list = sentence.split() reversed_list = word_list[:: -1] reversed_sentence = " ".join(reversed_list) print(reversed_sentence)
Scaler
scaler.com › home › topics › reverse words in a string in data structures
Reverse Words in a String in Data Structures - Scaler Topics
September 15, 2023 - In Java, we have the split method, which helps us split the string. We use it to split the string using the delimiter and reverse and store the line in another variable.
Educative
educative.io › answers › reverse-the-order-of-words-in-a-string
Reverse the order of words in a string
The parameter(s) of the reverse function will depend on the algorithm in the respective language. In C++, the function takes a start and an end pointer to the string as parameters and reverses it by swapping the first and the last letters; it then swaps the second and the second to the last letter, and so on.