ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Solidity: String Reversal

Solidity

Goal -- WPM

Ready
Exercise Algorithm Area
1pragma solidity ^0.8.0;
2
3contract StringUtilities {
4
5// Helper function to convert string to bytes
6function stringToBytes(string memory s) internal pure returns (bytes memory) {
7return bytes(s);
8}
9
10// Helper function to convert bytes to string
11function bytesToString(bytes memory b) internal pure returns (string memory) {
12return string(b);
13}
14
15// Function to reverse a string
16function reverseString(string memory s) public pure returns (string memory) {
17if (s.length == 0) {
18return "";
19}
20
21bytes memory b = stringToBytes(s);
22uint256 length = b.length;
23uint256 halfLength = length / 2;
24
25for (uint256 i = 0; i < halfLength; i++) {
26// Swap characters from the beginning and end
27bytes1 temp = b[i];
28b[i] = b[length - 1 - i];
29b[length - 1 - i] = temp;
30}
31
32return bytesToString(b);
33}
34
35// Example of a string with special characters
36function reverseSpecialString() public pure returns (string memory) {
37string memory special = "!@#$%^&*()_+";
38return reverseString(special);
39}
40}
Algorithm description viewbox

Solidity: String Reversal

Algorithm description:

This Solidity contract provides a `reverseString` function that takes a string as input and returns its reversed version. It efficiently handles the reversal by converting the string to bytes, swapping characters from the ends inwards, and then converting it back to a string. This is useful for various text manipulation tasks within smart contracts, such as validating input formats or generating dynamic messages.

Algorithm explanation:

The `reverseString` function first checks if the input string `s` is empty. If it is, an empty string is returned immediately, handling this edge case. Otherwise, the string is converted into a `bytes` array using `bytes(s)`. The length of the byte array is obtained, and `halfLength` is calculated as `length / 2`. A `for` loop iterates from `i = 0` up to `halfLength - 1`. In each iteration, it swaps the character at index `i` with the character at index `length - 1 - i`. This is done using a temporary `bytes1` variable. After the loop completes, the byte array `b` contains the reversed sequence of characters. Finally, the reversed byte array is converted back into a string using `string(b)` and returned. The time complexity is O(n), where n is the length of the string, because the loop iterates through approximately half of the string's characters. The space complexity is O(n) due to the creation of a new `bytes` array to hold the string's content.

Pseudocode:

function reverseString(s):
  if s is empty:
    return ""

  b = convert_to_bytes(s)
  length = length(b)
  halfLength = length / 2

  for i from 0 to halfLength - 1:
    temp = b[i]
    b[i] = b[length - 1 - i]
    b[length - 1 - i] = temp

  return convert_to_string(b)