How to resolve the algorithm Reverse words in a string step by step in the Racket programming language
How to resolve the algorithm Reverse words in a string step by step in the Racket programming language
Table of Contents
Problem Statement
Reverse the order of all tokens in each of a number of strings and display the result; the order of characters within a token should not be modified.
Hey you, Bub! would be shown reversed as: Bub! you, Hey
Tokens are any non-space characters separated by spaces (formally, white-space); the visible punctuation form part of the word within which it is located and should not be modified. You may assume that there are no significant non-visible characters in the input. Multiple or superfluous spaces may be compressed into a single space. Some strings have no tokens, so an empty string (or one just containing spaces) would be the result. Display the strings in order (1st, 2nd, 3rd, ···), and one string per line. (You can consider the ten strings as ten lines, and the tokens as words.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Reverse words in a string step by step in the Racket programming language
Source code in the racket programming language
#lang racket/base
(require racket/string)
(define (split-reverse str)
(string-join
(reverse
(string-split str))))
(define poem
"---------- Ice and Fire ------------
fire, in end will world the say Some
ice. in say Some
desire of tasted I've what From
fire. favor who those with hold I
... elided paragraph last ...
Frost Robert -----------------------")
(let ([poem-port (open-input-string poem)])
(let loop ([l (read-line poem-port)])
(unless (eof-object? l)
(begin (displayln (split-reverse l))
(loop (read-line poem-port))))))
#lang sweet-exp racket/base
require racket/string
define split-reverse(str)
string-join $ reverse $ string-split str
define poem
"---------- Ice and Fire ------------
fire, in end will world the say Some
ice. in say Some
desire of tasted I've what From
fire. favor who those with hold I
... elided paragraph last ...
Frost Robert -----------------------"
let
\\
poem-port $ open-input-string poem
let loop
\\
l $ read-line poem-port
unless eof-object?(l)
begin
displayln split-reverse(l)
loop read-line(poem-port)
You may also check:How to resolve the algorithm Rate counter step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the C++ programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the BCPL programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the Bracmat programming language