How to resolve the algorithm String matching step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm String matching step by step in the Rust programming language
Table of Contents
Problem Statement
Given two strings, demonstrate the following three types of string matching:
Optional requirements:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm String matching step by step in the Rust programming language
Source code in the rust programming language
fn print_match(possible_match: Option<usize>) {
match possible_match {
Some(match_pos) => println!("Found match at pos {}", match_pos),
None => println!("Did not find any matches")
}
}
fn main() {
let s1 = "abcd";
let s2 = "abab";
let s3 = "ab";
// Determining if the first string starts with second string
assert!(s1.starts_with(s3));
// Determining if the first string contains the second string at any location
assert!(s1.contains(s3));
// Print the location of the match
print_match(s1.find(s3)); // Found match at pos 0
print_match(s1.find(s2)); // Did not find any matches
// Determining if the first string ends with the second string
assert!(s2.ends_with(s3));
}
fn main(){
let hello = String::from("Hello world");
println!(" Start with \"he\" {} \n Ends with \"rd\" {}\n Contains \"wi\" {}",
hello.starts_with("He"),
hello.ends_with("ld"),
hello.contains("wi"));
}
You may also check:How to resolve the algorithm Array concatenation step by step in the BASIC256 programming language
You may also check:How to resolve the algorithm Curzon numbers step by step in the Pascal programming language
You may also check:How to resolve the algorithm Bioinformatics/Sequence mutation step by step in the Swift programming language
You may also check:How to resolve the algorithm Naming conventions step by step in the Tcl programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the PL/I programming language