diff --git a/codes/Rust/is_palindrome.rs b/codes/Rust/is_palindrome.rs new file mode 100644 index 0000000..da3f759 --- /dev/null +++ b/codes/Rust/is_palindrome.rs @@ -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::>(); + 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 +} \ No newline at end of file diff --git a/codes/Rust/max_number.rs b/codes/Rust/max_number.rs new file mode 100644 index 0000000..ba36919 --- /dev/null +++ b/codes/Rust/max_number.rs @@ -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::().ok()).collect::>(); + } + 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 +} \ No newline at end of file