Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> The first is useful for basic string operations.

Reversing a string is what I would consider basic string operations, but I also expect it not to break emoji and other grapheme clusters.

Nothing is easy.



Personally I'd consider reversing a string to be a pretty niche use case and, as you say, it's a complex operation. Especially in some languages.

Tbh I can't think of a time when I've actually needed to do that.


This always comes up as a thing that people use as an interview question or an algorithm problem. But for what conceivable non-toy reason would you actually want to reverse a string?

I have never once wanted to actually do this in real code, with real strings.

Furthermore, the few times I've tried to do something like this by being cute and encoding data in a string, I never get outside the ASCII character set.


To index from the end of the string. If you want the last 3 characters from a string, it's often easier to reverse the string and take the first 3.


Most languages that are good enough at doing Unicode are also modern enough to give you a “suffix” function.


And even ASCII can't do a completely dumb string reverse, because of at least \r\n.


Trivia/history question: There's a very good reason it's \r\n and not \n\r.


The commands were used to drive a teletype machine. Carriage return and newline were independent operations with carriage return physically taking longer. So \r\n allowed it to get to the next letter soon.


Generating a list through recursion often involves reversing it at the end, so I’ve reversed a lot of Erlang strings in real code.


Outside of an interview have you ever needed to reverse a string?


Sorting by reversed string is not totally uncommon.


What is a use case for this?


Another use case is finding words with a common ending. Can be used for finding similar inflections or rhymes (in which case you'd use some form of phonetic alphabet, which fits in normal ASCII, so the problem wouldn't arise there). I also once used it for a (very) quick&dirty spell correction algorithm, which had to run in less than 1s and had to read all data from CD-ROM.


I did this yesterday.

    find ... | rev | sort | rev > sorted-filelist
I had several directories and I wanted to pull out the unique list of certain files (TrueType fonts) across all of them, regardless of which subdirs they were in. (I'm omitting the find CLI args for clarity; the command just finds all the *.ttf (case-insensitive) files in the dirs.)

By reversing the lines before sorting them (and un-reversing them) they come out sorted and grouped by filename.


That's clever. But in a programming language (instead of a composition of coreutils), I would use a filename() function or something.


I have my personal library of string functions

Recently I rewrote the codepoint reverse function to make it faster. The trick is to not write codepoints, but copy the bytes.

One the first attempt I introduced two bugs.


I agree. Also, truncating a string won't work. The first 3 characters of "Österreich" are not "Ös" in my opinion.


> Reversing a string

I wonder: several comments are saying it hardly makes any sense to reverse a string but... Certainly there are useful algorithms out there which do work by, at some point, reversing strings no!? I mean: not just for the sake of reversing it but for lookup/parsing or I don't know what.


Some parsing algorithms may want to reverse the bytes but that's different to reversing the order of user-perceived characters.


Reversing a string does not make sense in most languages.


Sorting filenames by extension


If you reverse a filename string, you'd be sorting by a reversed extension. Most mature programming language infrastructures have a FilenameExtension() function or similar which in this case would be the best to use.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: