Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

.

Addendum

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.

share|improve this question

closed as not constructive by MichaelT, gnat, thorsten müller, Jalayn, World Engineer Apr 17 '13 at 13:23

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

5  
To send a text message that could be read in the rearview mirror while driving, so the cops won't notice. –  JeffO Dec 8 '10 at 19:17
1  
i use string reversal for reversing strings ;) –  Muad'Dib Dec 8 '10 at 23:54

16 Answers 16

up vote 15 down vote accepted

Sexegers

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

share|improve this answer

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.

share|improve this answer
public bool IsPalindrome(string toCheck)
{
    return toCheck == toCheck.Reverse();
}
share|improve this answer
4  
I suppose while a "palindrome checker" isn't notably useful, "write a palindrome checker" at least has a purpose. –  clockworkgeek Dec 8 '10 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 –  Jack Marchetti Dec 9 '10 at 5:26

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.

share|improve this answer
1  
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 Dec 8 '10 at 21:33

Interviews!

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.

share|improve this 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.....

share|improve this answer
<span style="unicode-bidi: bidi-override; direction:rtl;">
    <?php echo strrev($emailaddress); ?>
</span>

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.

share|improve this answer
1  
And the most popular obfuscation in use was only given a single sentence in that whole article... Encoding as an image. –  clockworkgeek Dec 8 '10 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. –  NickC Dec 8 '10 at 19:39

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.

share|improve this answer

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.

share|improve this answer
1  
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. –  Omega Centauri Dec 8 '10 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...

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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.

myproject::SomeClass::GetFoo
myproject::SomeClass::GetBar

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 :)

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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