Hone logo
Hone
Problems

Simple String Utilities Library

This challenge asks you to create a simple library crate in Rust that provides a few utility functions for manipulating strings. Building a library crate is a fundamental skill in Rust development, allowing you to encapsulate reusable code and share it across multiple projects. This exercise will reinforce your understanding of Rust's module system, visibility, and crate structure.

Problem Description

You are tasked with creating a new Rust crate named string_utils that provides the following functionalities:

  1. reverse_string(s: &str) -> String: This function takes a string slice (&str) as input and returns a new String containing the reversed version of the input string.
  2. count_words(s: &str) -> usize: This function takes a string slice (&str) as input and returns the number of words in the string. Words are separated by whitespace (spaces, tabs, newlines). Leading and trailing whitespace should be ignored.
  3. is_palindrome(s: &str) -> bool: This function takes a string slice (&str) as input and returns true if the string is a palindrome (reads the same forwards and backward, ignoring case and non-alphanumeric characters), and false otherwise.

The crate should be structured with a public module named string_utils containing these functions. All functions should be publicly accessible.

Examples

Example 1:

Input: "hello"
Output: "olleh"
Explanation: The string "hello" reversed is "olleh".

Example 2:

Input: "This is a test"
Output: 4
Explanation: The string "This is a test" contains four words: "This", "is", "a", and "test".

Example 3:

Input: "A man, a plan, a canal: Panama"
Output: true
Explanation: Ignoring case and non-alphanumeric characters, the string becomes "amanaplanacanalpanama", which is a palindrome.

Example 4:

Input: "race a car"
Output: false
Explanation: Ignoring case, the string becomes "raceacar", which is not a palindrome.

Constraints

  • The input string s can be of any length, including empty strings.
  • The reverse_string function should allocate a new String and not modify the original input.
  • The count_words function should handle strings with multiple spaces between words correctly.
  • The is_palindrome function should ignore case and non-alphanumeric characters (letters and numbers only). All other characters should be ignored when determining if a string is a palindrome.
  • Performance: The functions should be reasonably efficient for typical string lengths (up to a few thousand characters). Avoid excessively complex algorithms unless necessary.

Notes

  • You will need to create a new Rust project and structure it as a library crate.
  • Use Rust's standard library functions for string manipulation.
  • Consider using iterators and functional programming techniques for concise and efficient code.
  • Remember to include a Cargo.toml file with the necessary dependencies and crate metadata.
  • The is_palindrome function will require filtering out non-alphanumeric characters and converting the string to lowercase before comparison. You can use the char type and its methods for this purpose.
  • Test your functions thoroughly with various inputs, including edge cases like empty strings, strings with only whitespace, and strings with mixed case and special characters.
Loading editor...
rust