Reverse words in a string using lambda expressions

reverse string using lambda expression python
how to reverse a word in python
python reverse letters in a word
reverse sentence in java without using split
reverse a number in python using slicing

So I was doing some standard programming interview questions and came across this one:

Reverse words in a string (words are separated by one or more spaces)

I thought it would be fun to do it using lambda expressions, but I found it difficult to get right and my solution feels kind of unwieldy and hacky. So now my question is: Is there a more idiomatic solution than mine?

public static String reverseWordsInString(String str)
{
    StringBuilder sb = new StringBuilder();

    Stream.of(str.split(" ")).forEach(
        word -> sb.append(reverseWord(word, !str.endsWith(word)))
    );
    return sb.toString();
}
public static String reverseWord(String str, boolean trailingSpace)
{
    StringBuilder sb = new StringBuilder();

    for(int i = str.length() - 1; i >= 0; i--)
    {
        sb.append(str.charAt(i));
    }
    sb.append(trailingSpace ? " " : "");
    return sb.toString();
}

Your task description is a bit ambiguous. I couldn’t recognize whether you have to reverse the words or reverse the characters of the words. But here are solutions for both:

public static String reverseWordsInString(String str) {
    return String.join(" ", Pattern.compile(" +").splitAsStream(str)
        .collect(LinkedList::new, LinkedList::addFirst, (a,b)->a.addAll(0, b)));
}

 

public static String reverseWordCharacters(String str) {
    return Pattern.compile(" +").splitAsStream(str)
        .map(word->new StringBuilder(word).reverse())
        .collect(Collectors.joining(" "));
}

The first one will reverse the words of a string, thus reverseWordsInString("this is an example") will return example an is this, the second one will reverse the characters of each word, so reverseWordCharacters("this is an example") yield siht si na elpmaxe.

java - Reverse words in a string using lambda expressions, Reverse words in a string (words are separated by one or more spaces). I thought it would be fun to do it using lambda expressions, but I found  Reverse words in a string (words are separated by one or more spaces) I thought it would be fun to do it using lambda expressions, but I found it difficult to get right and my solution feels kind of unwieldy and hacky.

Following pvg remark Java 8 stream reverse order answer this but it’s hard to figure it out from the question itself as it refers to ints. So based on Venkata Raju answer in the above:

Arrays.stream(sentance.split("\\s+"))
         .collect(Collectors.toCollection(LinkedList::new))
         .descendingIterator().forEachRemaining(System.out::println);

Split and collect the result to an intermediate linked list and iterate on it in reverse order

How to reverse a string using lambda expression in Java?, We can reverse a string entered by the user using the charAt() method of String class to extract characters from the string and append them in reverse order to reverse the entered string. In the below example, we need to reverse a string using lambda expression with the help of the Scanner class. Using reverse, this LINQ (Lambda Expression) sample in C# reverts characters in array.

The following should work in python where message is the message

def reverse_words(message):
    return ' '.join([x[::-1] for x in message[::-1].split(' ')])

Reverse strings using lambda expressions, This block lambda reverses the characters in. a string. StringFunc reverse = (str) -​> {. String result = "" nextLine();. System.out.println(word+" in reversed form - ". Given a String is a set of words separated by space. This code returns each word count that how many times repeated in a string. Create a word string array using a regular expression. Next is to create a Stream array using Arrays.stream(array) method.

In Java 8, how can we reverse a single string using lambdas and , Check if a string contains only alphabets in Java using Lambda expression · Check if a Java program to print Even length words in a String · In Java, Can we call the StringBuffer.reverse() is an inbuilt method which is used to reverse the  Initially, reverse the individual words of the given string one by one, for the above example, after reversing individual words the string should be “i ekil siht margorp yrev hcum”. Reverse the whole string from start to end to get the desired output “much very program this like i” in the above example.

StringBuffer reverse() Method in Java with Examples, If you run the reverse method above, it will produce a String like this: Published at DZone with permission of Jonatan Ivanov . See the In other words, you can't implement your function as: Serializable Java Lambdas. In these java programs, learn to reverse the words of a string in Java without using api functions. We can reverse the words of string in two ways: In this example, we will follow below steps to reverse the characters of each word in it’s place. Read string from input. Split the string by using space as delimiter. Loop through all words.

The Right Way to Reverse a String in Java, In short, the lambda expression for evaluating aggregates accepts two Here's an interesting example of a LINQ expression that reverses the words in a sentence. The code starts by splitting the sentence into words, which are returned in an  C# Lambda Expressions; C# program to Reverse words in a string. //Add each word to the result string in reverse order since a stack is a FIFO data structure

Comments
  • stackoverflow.com/questions/24010109/… (also, your split is wrong)
  • Thanks, your last example was exactly what I was hoping for. Very insightful. :)
  • Did you test this?