String reverse using wasm and rust

Please provide me a sample code for reversing a string and check the input string is palindrome using rust and wasm

fn is_palindrome(input: &str) -> bool {
    input == reverse(input)
}

fn reverse(input: &str) -> String {
    input.chars().rev().collect()
}
1 Like

Doing this properly is hard. Find a crate.

Otherwise you'll hit problems like this, using the reverse above:
image
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0eaa8c1c024fd629da3f3f82a8597152

3 Likes

Of course. The actual code should be tailored for the case at hand. For example: do you consider ëte a palindrome? What about German ae, oe and ue? Do you consider their reverse to be ea, eo and eu, or a, e and u? Or ä, ö and ü? Consider my answer to be a hint to build on to encode your specific requirements.

1 Like

this sounds like a literal homework question :woman_shrugging:

If for some reason you need to do grapheme-cluster-aware string reversal in a no_std environment, there's a crate for that too.