Peter’s blog ✴ Week 379 ✴ 22 June 2026

THE WEEKLY CHALLENGE
Reversing and curious numbers

The Perl Camel

Task 1

Reverse string

You are given a string. Write a script to reverse the given string without using standard reverse function.

Examples


Example 1
Input: $str = ''
Output: ''

Example 2
Input: $str = 'reverse the given string'
Output: 'gnirts nevig eht esrever'

Example 3
Input: $str = 'Perl is Awesome'
Output: 'emosewA si lreP'

Example 4
Input: $str = 'v1.0.0-Beta!'
Output: '!ateB-0.0.1v'

Example 5
Input: $str = 'racecar'
Output: 'racecar'

Analysis

Well, dead easy, I thought and of course there are several dead easy ways to do it. I have taken advantage of using substr() as an lvalue and simply moved the characters in reverse sequence.

But as usual I thought about efficiency.

My first observation is that it's probably a bad idea to alter the input string. In some, maybe every, case that will result in the remaining string being copied once for each character, which is unnecessary and thus inefficient.

So let's copy (not remove) the characters from the input string, starting at the end. We can then append them each to a new string and achieve the desired result. Is that better? Probably.

But depending on the underlying architecture, because the growing output string will probably have been allocated a fixed length in bytes in memory, if you add to its end the whole string may need to be copied to a longer location each time you add a character.

So my next thought was first to create an output string the same length as the input string, but filled with spaces:

$string = 'abcde'; $output = '     ';

Now if we copy the characters one by one there will be no need for the system to extend $output, so it can simply update it in place.

But there is a snag, and that is non-ASCII characters. As we know, these are held as multibyte entities, so our $output made up of blanks will not be long enough if $string contains, say, £ or ¬ - which are characters on a UK keyboard - or thousands of other characters with Unicode representations.

And I can't think of an easy way around that in Perl, and it's rather hot here, so let's leave it at that.

Try it 

Your input:



eg: An easy challenge?

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-06-22
use utf8;     # Week 379 - task 1 - Reverse string
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

reverse_string('abcde');
reverse_string('The quick brown fox jumps over the lazy dog');
reverse_string('Linke Grüne wollen mehr Geld für ärmere Rentner');
reverse_string('');

sub reverse_string {
    
    # initialise
    my ($string, $gnirts, $last);
    $string = $_[0];
    $last = length($string) - 1;
    $gnirts = '';
    
    # reverse string
    substr($gnirts, $_, 1) = substr($string, $last - $_, 1) for 0 .. $last;
    
    # report
    say qq[\nInput:  '$string'];
    say qq[Output: '$gnirts'];  
}

8 lines of code

Output from script


Input:  'abcde'
Output: 'edcba'

Input:  'The quick brown fox jumps over the lazy dog'
Output: 'god yzal eht revo spmuj xof nworb kciuq ehT'

Input:  'Linke Grüne wollen mehr Geld für ärmere Rentner'
Output: 'rentneR eremrä rüf dleG rhem nellow enürG ekniL'

Input:  ''
Output: ''

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain