Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: Add a palindrome checker and a max number finder #338

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions codes/Rust/is_palindrome.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::io::{self, Write};

fn main() {
print!("Input your string: ");
std::io::stdout().flush().expect("Failed to flush the console.");
let line = input();
let mut string = line.trim().to_string();
print!("Do you want the check to be case sensitive (Y for yes, anything else for no): ");
std::io::stdout().flush().expect("Failed to flush the console.");
let line = input();
let case_sensitive = line.trim().to_lowercase() == "y";
if !case_sensitive {
string = string.to_lowercase();
}
let characters = string.chars().collect::<Vec<char>>();
let characters_length = characters.len();
for i in 0..(characters_length + 1) / 2 {
if characters[i] != characters[characters_length - i - 1] {
println!("It is NOT a palindrome");
return;
}
}
println!("It is a palindrome");
}

fn input() -> String {
let mut line = String::new();
io::stdin().read_line(&mut line).expect("Failed to read input line");
line
}
19 changes: 19 additions & 0 deletions codes/Rust/max_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::io::{self, Write};

fn main() {
let mut numbers = vec![];
while numbers.is_empty() {
print!("Enter your numbers separated with spaces: ");
io::stdout().flush().expect("Failed to flush console.");
let line = input();
numbers = line.trim().split(' ').filter_map(|x| x.parse::<i32>().ok()).collect::<Vec<i32>>();
}
let max_number = numbers.iter().max().expect("Failed to get the maximum number.");
println!("Maximum number from {:?} is {}", numbers, max_number);
}

fn input() -> String {
let mut line = String::new();
io::stdin().read_line(&mut line).expect("Failed to read input line");
line
}