Skip to content

Instantly share code, notes, and snippets.

@adityasuseno
Created December 22, 2024 11:35
Show Gist options
  • Select an option

  • Save adityasuseno/a0664d868a3ae30684b560a09613ecad to your computer and use it in GitHub Desktop.

Select an option

Save adityasuseno/a0664d868a3ae30684b560a09613ecad to your computer and use it in GitHub Desktop.
Simple Palindrome Program to Spell a given word backwards. This time is not case sensitive.
/* Usage: Palindrome <YourWord> */
use std::env;
fn main () {
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
println!("Original Word is {}", args[1]);
println!("That Words Contains {} characters", args[1].len());
print!("Let's spell it backwards: ");
for i in args[1].chars().rev() {
print!("{}", i);
}
println!("");
if is_palindrome(&args[1]) {
println!("It is a Palindrome");
}
else {
println!("It is Not a Palindrome");
}
}
else {
println!("Usage: Palindrome <YourWords>");
}
}
fn is_palindrome(s: &str) -> bool {
let s_lower = s.to_lowercase(); // Convert the string to lowercase to ignore case sensitivity
s_lower.chars().eq(s_lower.chars().rev()) // Check if the reversed string is equal to the original string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment