-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathis_palindrome.rs
30 lines (28 loc) · 1019 Bytes
/
is_palindrome.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
}