Write a Rust program to check if a given string is a palindrome.
Write a Rust program to check if a given string is a palindrome.
417
20-Jun-2023
Aryan Kumar
24-Jun-2023Sure, here is a Rust program to check if a given string is a palindrome:
Rust
This program first initializes a boolean variable to track if the string is a palindrome. The program then iterates over the string backwards. For each character in the loop, the program checks if the character is not equal to the character at the same index from the beginning of the string. If the character is not equal, then the program sets the boolean variable to false and breaks out of the loop. Finally, the program returns the boolean variable.
To run this program, you can save it as a
.rsfile and then compile and run it using the following commands:Code snippet
This will prompt you to enter a string. Enter a string and press Enter. The program will then print whether the string is a palindrome or not.
For example, if you enter the string "racecar", the program will print the following output:
Code snippet