Mediocre Electron Herding

What even is code

Reverse a String

I thought I'd blast off this section with one of the oldest puzzles.

Javascript Solutions

I guess its obligatory to put javascript at the top, this being a site made in react and all that.

Conventional version
function reverse(inputString)
{
const reversed = [];
const inLength = inputString.length - 1;
for(let i = inLength; i >= 0; i--)
{
reversed.push(inputString[i]);
}
return reversed.join('');
}
Simpler with builtin methods
function reverse(inputString)
{
return inputString.split("").reverse().join("");
}
Simpler but UTF-16 friendly

This uses destructuring assignment to avoid the whole problem with invalid unicode strings.

function reverse(inputString)
{
return [...inputString].reverse().join("");
}

Java Solutions

Obviously java is the most problematic one in this list so I am just going to cheat.

Cheating with StringBuffer
public String reverseString(String inputString)
{
StringBuffer sb = new StringBuffer(inputString);
return sb.reverse();
}

C++ Solutions

C++ has a bit more variety on how you can approach it as it has some built in methods in standard library to make this puzzle pretty easy.

C++ with reverse (also known as cheating)
std::string reverse_string(std::string input)
{
std::reverse(input.begin(), input.end());
return input;
}
C++ with swap (I guess this is also cheating?)
std::string reverse_string(std::string input)
{
int inLength = input.length();
for (int i = 0; i < (inLength / 2); i++)
{
std::swap(input[i], input[inLength - i - 1]);
}
return input;
}
C++ With no swap
std::string reverse_string(std::string input)
{
int inLength = input.length();
int n = inLength - 1;
for (int i = 0; i < (inLength / 2); i++)
{
char t = input[i];
input[i] = input[n];
input[n] = t;
n = n - 1;
}
return input;
}

C# Solutions

I mean I guess its possible to just loop through it like I did in C++, but I like this solution more using LINQ magic:

new string(inputString.Reverse().toArray());

This abuses the fact that strings can act like Enumerables. However to use this you need to be "using" System.Linq. This however is TERRIFYINGLY inefficent.

so just use this instead:

new string(Array.Reverse(inputString.toCharArray()));

LUA solution

I thought I'd put this here as I remember having trouble with this method specifically back in the olden days of gmod 🙁.

(string.reverse(inputString))
I don't wanna talk about it.