How to resolve the algorithm Phrase reversals step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Phrase reversals step by step in the Rust 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 Rust programming language
Source code in the rust programming language
fn reverse_string(string: &str) -> String {
string.chars().rev().collect::<String>()
}
fn reverse_words(string: &str) -> String {
string
.split_whitespace()
.map(|x| x.chars().rev().collect::<String>())
.collect::<Vec<String>>()
.join(" ")
}
fn reverse_word_order(string: &str) -> String {
string
.split_whitespace()
.rev()
.collect::<Vec<&str>>()
.join(" ")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_reverse_string() {
let string = "rosetta code phrase reversal";
assert_eq!(
reverse_string(string.clone()),
"lasrever esarhp edoc attesor"
);
}
#[test]
fn test_reverse_words() {
let string = "rosetta code phrase reversal";
assert_eq!(
reverse_words(string.clone()),
"attesor edoc esarhp lasrever"
);
}
#[test]
fn test_reverse_word_order() {
let string = "rosetta code phrase reversal";
assert_eq!(
reverse_word_order(string.clone()),
"reversal phrase code rosetta"
);
}
}
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Arturo programming language
You may also check:How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Fixed length records step by step in the J programming language
You may also check:How to resolve the algorithm Comments step by step in the KonsolScript programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the M2000 Interpreter programming language