How to resolve the algorithm Phrase reversals step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Phrase reversals step by step in the Nim programming language

Table of Contents

Problem Statement

Given a string of space separated words containing the following phrase:

Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Phrase reversals step by step in the Nim programming language

Source code in the nim programming language

import algorithm, sequtils, strutils

const Phrase = "rosetta code phrase reversal"

echo "Phrase:              ", Phrase
echo "Reversed phrase:     ", reversed(Phrase).join()
echo "Reversed words:      ", Phrase.split().mapIt(reversed(it).join()).join(" ")
echo "Reversed word order: ", reversed(Phrase.split()).join(" ")


import strutils

const Phrase = "rosetta code phrase reversal"

proc reversed(s: string): string =
  for i in countdown(s.high, 0):
    result.add s[i]

proc reversedWords(s: string): string =
  let words = s.split()
  result = reversed(words[0])
  for i in 1..words.high:
    result.add ' ' & reversed(words[i])

proc reversedWordOrder(s: string): string =
  let words = s.split()
  result = words[^1]
  for i in countdown(words.high - 1, 0):
    result.add ' ' & words[i]

echo "Phrase:              ", Phrase
echo "Reversed phrase:     ", reversed(Phrase)
echo "Reversed words:      ", reversedWords(Phrase)
echo "Reversed word order: ", reversedWordOrder(Phrase)


  

You may also check:How to resolve the algorithm First-class functions step by step in the Wren programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Quackery programming language
You may also check:How to resolve the algorithm User input/Text step by step in the smart BASIC programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the VBA programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the Scala programming language