Rust Program to Check Palindrome

Check Palindrome

Rust Programming Language

Check Palindrome


Rust program that checks if a given string is a palindrome or not.


use std::io;

fn main() {
    println!("Please enter a string:");

    let mut input = String::new();
        .read_line(&mut input)
        .expect("Failed to read line");

    let input = input.trim();
    let reversed_input = input.chars().rev().collect::<String>();

    if input == reversed_input {
        println!("The string is a palindrome");
    } else {
        println!("The string is not a palindrome");


Please enter a string:
The string is a palindrome{codeBox}


This program prompts the user to enter a string, reads the input string from the user using the io module from Rust's standard library, and trims any leading or trailing whitespace from the input string.

We then reverse the input string using the rev() method of the Chars iterator and collect the result back into a new String using collect::<String>().

Finally, we check if the input string is equal to the reversed input string. If they are equal, we print "The string is a palindrome" to the console. Otherwise, we print "The string is not a palindrome".

This shows the program prompting the user to enter a string, the user entering the string racecar, and the program checking if the string is a palindrome (which it is), and displaying the appropriate message to the console.

Post a Comment