Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search

Reverse a String in Every Language

Written byJeremy Grifski inCode Published April 19, 2018Last Updated August 30, 2020
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 asliceOpens in a new tab.:

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

If you’re interested in trying this in your favorite language, why not check outthe 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 thesample 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 offlanguage 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 asRosetta 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.

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. Most recently, he earned a PhD in Engineering Education and now works as a Lecturer. In his spare time, Jeremy enjoys spending time with his wife and kid, playing Overwatch 2, Lethal Company, and Baldur's Gate 3, reading manga, watching Penguins hockey, and traveling the world.

Recent Posts

link to Life Update: I'm Doing Well

Life Update: I'm Doing Well

I wanted to take a moment to just talk about how I'm doing, which is really well actually.

link to The Worst Use Cases for Generative AI That Are Already Mainstream

The Worst Use Cases for Generative AI That Are Already Mainstream

This article looks at several use cases of generative AI that I find dystopian at best, and I want to talk about them.

About Me

Welcome to The Renegade Coder, a coding curriculum website run by myself,Jeremy Grifski. If you like what you see, considersubscribing to my newsletter. Right now,new subscribers will receive a copy of my Python 3 Beginner Cheat Sheet. If newsletters aren't your thing, there are at least 4 other waysyou can help grow The Renegade Coder. I appreciate the support!

Legal

The Renegade Coder is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com.






Longest Active Series


[8]ページ先頭

©2009-2025 Movatter.jp