Reverse a String in Every Language

Reverse a String in Every Language Featured Image

Welcome to Reverse a String in Every Language: a series of articles demonstrating string reversal in as many languages as possible.

Table of Contents

What is Reverse a String?

In code, string reversal is the process of flipping the characters in a string such that the resulting string contains all the original characters but in reverse order. For example, let’s say we have some string:

"Hello, World!"

If we reverse this string, we end up with the following string:

"!dlroW ,olleH"

As we can see, we’ve generated a new string with each character in the reverse order.

Often times, the solution for this kind of problem is to grab the character array and produce a new string by reading the original string in reverse. In some cases, it’s even possible to reverse that array in place.

I’m sure you’ll find many different implementations throughout this collection. Personally, I’m fond of python which uses a sliceOpens in a new tab.:

import sys

if len(sys.argv) > 1:
    print(sys.argv[1][::-1])

If you’re interested in trying this in your favorite language, why not check out the documentationOpens in a new tab. for our specific set of rules? Once you’re ready, fork the repo and make a pull request with your changes.

Why Reverse a String in Every Language?

When I started building up the sample programs repositoryOpens in a new tab., I only had one code snippet for each language: Hello World. While Hello World is a fun program, it doesn’t do a great job of showing off language features. As a result, I decided to expand the repository to support other kinds of programs.

Of course, there are a couple archives that already exist for more advanced snippets:

  1. 99 Bottles of BeerOpens in a new tab.
  2. FizzBuzzOpens in a new tab.

In fact, I’m probably wasting my time as Rosetta CodeOpens in a new tab. already hosts the largest database of coding snippets I’ve ever seen. That said, I’m not here to compete, I’m here to learn.

Regardless, where else are you going to find a place to read my ramblings about every language?

An Alphabetical List of Languages

As with the Hello World series, I don’t release articles in any sort of order. To help provide some sort of easy to use archive, I’ve decided to list all of the Reverse a String implementations here in alphabetical order:

  1. Reverse a String in Java
  2. Reverse a String in Python
  3. Reverse a String in Ruby
  4. Reverse a String in Scheme
  5. Reverse a String in Swift

Once again, thank you for your support. If there’s a language you’d like to see in this series, drop it down below in the comments. Also, don’t forget to share this article with your friends. It helps the series grow!

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

← Previous Post: [#38] [#40]: Next Post →

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts