In PHP it's strrev(), in Rails it's .reverse, but most languages don't have any string reverse function. Some have array reverse functions that can be used on characters. I was thinking this must be a major oversight but then it occurred to me, what would you actually use string reverse for???

The only time I can think I've seen it is in demos and lessons to turn "Hello World!" into "!dlroW olleH".

My question is; Is there a use for reversing a string or is it completely pointless?



There have been way more answers that I had anticipated and not all of them were entirely academic. I would have put money that no one could come up with a legitimate example. I also didn't think I would learn anything new but Mark Canlas' regex suggestion is just brilliant and I look forward to the opportunity for it to prove itself. Thanks to all.

  • @clockworkgeek - if you ask a candidate to do string reversal in their favorite language, you'd be surprised how many don't come up with the basic function you mentioned. Then how many can't come up with a loop to implement it themselves.
    – justkt
    Commented Dec 8, 2010 at 18:30
  • @justkt - That is a whole other question waiting to happen but might be a topic for TopCoder instead. Commented Dec 8, 2010 at 19:13
  • 6
    To send a text message that could be read in the rearview mirror while driving, so the cops won't notice.
    – JeffO
    Commented Dec 8, 2010 at 19:17
  • @justkt - If I had to write an iterative loop to do it I would start at opposite ends swapping chars until the middle is reached. But then how do you swap two values? Here is simply the best answer I've ever been given: a ^= b; b ^= a; a ^= b; Commented Dec 8, 2010 at 19:20
  • 1
    i use string reversal for reversing strings ;)
    – Muad'Dib
    Commented Dec 8, 2010 at 23:54

16 Answers 16



Sometimes, problems that involve regular expressions can more easily be written by having the input string reversed and tackling the problem in a different way.

Technique courtesy of the man who taught me Perl.

Sexeger on PerlMonks

  • Thank you. A very handy trick for finding the last of something. Duly noted. Commented Dec 8, 2010 at 21:04
  • I've been doing this for years. Helps in parsing email addresses.
    – sal
    Commented Dec 8, 2010 at 22:18

Well, this is a tongue-in-cheek answer.

"Back in the day" I owned a Unix box, and it had an ordered dictionary file of English words, used for spell-checking.

I made a new file by reversing all the words in the dictionary, sorting it, and then reversing them again. The result was a list of words sorted from right-to-left.

So if you searched it for a word, next to that word would be words with similar endings. So it was easy to make little poems!

You can really amuse yourself when you see what rhymes with what.


I have been a coder/developer/sysadmin for ~10 years and I cannot remember ever needing string reversal in real life situations.

The only immediate use-case that I can think of is number base conversion: done naively, the procedure returns a reversed string. However, with a bit of math, you can compute upfront the amount of space needed, so you can start filling the buffer from the end.

  • 2
    It's possible the bit of math is more expensive than reversing the string, so after benchmarking on your platform (ARM, MIPS, x86) you might use a string reverse. Maybe.
    – Zan Lynx
    Commented Dec 8, 2010 at 21:33
  • You can fill the buffer from the end and use a memmove to the start when finished. It's probably cheaper than calculating log(n)/log(base) to calculate the number of digits necessary. Commented Jan 17, 2017 at 12:43
public bool IsPalindrome(string toCheck)
    return toCheck == toCheck.Reverse();
  • 1
    Yep - we actually have an interview-screening where candidates write a palindrome checker and most do this. However, I like it better when the candidates iterate from 0 to n/2 and compare the character at the opposite end.
    – Nicole
    Commented Dec 8, 2010 at 17:57
  • @Renesis: You can also do: set p begining of string, q end of string, while ( (*p == *q) && (p <= q) {p++; q--} return p > q; in C and other pointer languages.
    – Michael K
    Commented Dec 8, 2010 at 18:14
  • 4
    I suppose while a "palindrome checker" isn't notably useful, "write a palindrome checker" at least has a purpose. Commented Dec 8, 2010 at 18:15
  • 2
    At an interview, they asked me to reverse a string and they said "remember this no string.Rverse(). So I just converted the string to a character array and did Array.Reverse Commented Dec 9, 2010 at 5:26
  • That's a pretty niche use and not one you would generally say would be worth including in language/library!
    – Dan Diplo
    Commented Dec 9, 2010 at 9:49


Reversing a string (in place or not) is a very common interview question for basic programming knowledge. A language lacking these built in functions would be difficult to interview for. The candidate would actually have to know something.1

1: This is a tongue-in-cheek answer.


I've seen situations where a desktop application was talking with embedded devices and was switching byte-order endianness constantly, and data was moved around as strings. That's about it for me though.

I wouldn't have used strings for that application but thats just the way it was.....

  • +1 for this answer. It is at least a practical example although I too would have done it another way, perhaps by choosing primitive types that are differently enddian. Commented Dec 8, 2010 at 17:38

Anything where working with the reversed string is easier.

Working with integers as strings is a lot easier if the strings are reversed. I built some library functions for doing math with large integers and used string reversal to make the arithmetic functions simpler.

Granted, I've only used it for cranking out answers on Project Euler, but still, the original premise holds.

  • +1. when using string representations of numbers, reversed representation is very helpful. and usually the standard library will represent numbers in the normal order.
    – back2dos
    Commented Dec 8, 2010 at 22:09
<span style="unicode-bidi: bidi-override; direction:rtl;">
    <?php echo strrev($emailaddress); ?>

Not the best solution for obfuscating an email address, since when you add it to the clipboard it's still reversed. And, if it became popular, it would soon be detected by email scraping bots.

Still, it's been suggested.

  • 1
    And the most popular obfuscation in use was only given a single sentence in that whole article... Encoding as an image. Commented Dec 8, 2010 at 18:11
  • 1
    @clockworkgeek, it may be a popular one but IMHO it's the worst of the effective solutions - not super easy to generate, not embedded in HTML (speed, storage of images, server load), looks out of place, can't be styled with CSS, and same poor user experience as string reversal, of having to remember it and retype it. And probably more problems I'm not thinking of.
    – Nicole
    Commented Dec 8, 2010 at 19:39
  • Curiously though, using AT and DOT, the easiest to decode for a harvester, turns out to have almost perfect effectiveness in preventing harvesting. Sometimes keeping it simple isn't such a bad idea. Commented Dec 8, 2010 at 21:24

ASCII isn't the best encoding for genetic information (you can pack the base types ACGT as 2bits). Pack them into an array of longlongs and you get 32 genetic "letter" per word. DNA can get turned around, so you got to check a chunk of DNA against both forward are reverse copies of a test sequence. So being able to reverse a packed string of 2bit quantities can be very useful for various sorts of genetic analysis.

I had as an item on a benchmark for spy agencies, how fast can you reverse the bits it a long-long (actually a very long array of long-longs). The obvious method of exchanging 2bits at a time is much slower than the less obvious methods. These are related to some of the neat algorithms for in place array transposition.

Tangurena: The operation you refer to is called a population count. Similar desirables for bit packed data are leading and trailing zero count. There are a lot of really neat things that one can do with bit packed data. A single operation on a longlong is 64way data parallel, so if you know what you are doing you can get incredible performance for certain types of computataions.

  • Interesting topic, just how would you reverse a bit field? Commented Dec 8, 2010 at 21:06
  • "just how would you reverse a bit field?" Commented Dec 8, 2010 at 22:17
  • 2
    One approach is table lookup.You can bit reverse a byte by using a table. So you could do that on the indivdual bytes. There are also ways to move several bits and once... A bit of cleverness, and tradeoffs (table size versus number of operations etc.) and you can attempt to tune it. Commented Dec 8, 2010 at 22:20

Maybe low-cost multi-lingual support, for languages that use letters right-to-left (such as Arabic), instead of left-to-right. Of course you have to watch out for accent characters modifying the right character...


I dunno maybe someone has a burning need to check for palindrome's....

I don't think its completely useless, as there may well be situations where one needs to be able to reverse a string.

  • The follow up question to that would be, when have you ever needed to check a palindrome in the real world? Again, I've only ever seen anyone care in algorithm lessons. Commented Dec 8, 2010 at 17:19
  • like this one for example: jimsabo.com/palindrome.html
    – Darknight
    Commented Dec 8, 2010 at 17:26
  • I think there are probably other uses, but it's domain specific. For instance if string searches were optimized for forward searching and you wanted to do multiple searches for the last occurrence, you might want to reverse it first. This would be an optimization though, and shouldn't be done unless there's a provable case for it. I could envision an algorithm for generating a hash of a string value that wanted to use the tail end, possibly reversed, if it happened to provide better hashing characteristics. Commented Dec 8, 2010 at 17:26

In natural language processing & parsing, sometimes it is easier to search a string from the end to the beginning. A string reverse would be useful for debugging, or as an alternate way to write the loop (reverse the string and then loop from index 0 to n-1).

Also some languages are written right to left, so a string reverser could be used for that if you were in an environment that didn't natively recognize LTR/RTL languages.

A string (in some languages) is an array of chars, but it could just as well be paychecks or inventory modifications. In a loop moving across these, you might be do some calculations that should be the same regardless to what order you process them. A perfectly cromulent unit test would be to check to see if they the calculations apply the same going forward or going backward. This might be trivially obvious for addition, maybe not for other more opaque operations.


For compilers ?

It's amusing, but most symbols in a language will begin by a common pattern. I am not talking about Hungarian Notation here, but if you think about namespace / classes, then a lot of symbols will actually share a common prefix.


The problem is, when doing a binary search, common prefixes are the worst thing you can end up with, because you'll end up comparing those prefixes over and over again.

On the other hand, if you get a look at the strings backward, you'll see much more entropy! And then suddenly a binary search (over a Trie) gets much more powerful!

It's always bugged me that C++ mangled names (by gcc) were not reversed to put the namespace LAST :)


I flip phone numbers and certain strings for searches from time to time


The only time I can recall seeing string reversal being used was a function I saw way back that used it while parsing file names, to ensure that the '.' it found in the file name was in fact the last dot that seperated the file name from extension. ie, parsing a file name like data.2010.12.08.dat, you'd reverse the string, find the first dot, subtract that position from the end of the original string, and take the substring. I'm not saying thats the optimal way to do it, but thats what it did. It may have been in powerbuilder, where such wierd uses of functions were common to workaround various non-obvious problems.


The only real worls app I saw using the strrev was to store user passwords 'unreadable' in the database...

But I can remember there is a pattern in C to use the strrev, maybe I come up with it later.

Not the answer you're looking for? Browse other questions tagged or ask your own question.