4 ms·
Do people really need to reverse strings in the real world? I don't think I've ever written code to do that outside of homework assignments and interviews.
by billpg 13y ago
Do people really need to reverse strings in the real world?
I don't think I've ever written code to do that outside of homework assignments and interviews.
- markild 13y agoThe same mechanism that is used for reversing a string can be very useful though. Think in the lines of python's: >>> 'abcd'[:-1] 'abc'
- jmpe 13y agoHad such a case a few months back. Strings of single-byte characters are Endian-agnostic but multi-byte character encoding is affected by Endianness. To cope with it I read the sequence as single byte, then reversed, then changed the encoding to proper encoding and reversed again. The data came from a binary dump where I only needed a section that contained a few strings. I admit it's dirty but it was throwaway code for an isolated case. Edit: eh, guys, as I stated the string came from a binary dump. I didn't get to choose the encoding, it came from ROM in an embedded system with a different Endianness. I had to figure out a way to make it human readable.
- justincormack 13y agoUse UTF8, no endian issues. Thats yet another reason why UTF16 and UTF32 are broken.
- monkeyninja 13y agolanguage will not store unicode string internally with UTF8. Yes, we use it as input and output, but in memory, utf8 is terrible for random access characters. endian is only an issue (normally) for input and output, not really an issue for internal storage. especially when using UTF16 and UTF32 you know exactly the size of items.
- jeltz 13y agoYou cannot do random access at all in Unicode, not even UTF-32 (and absolutely not UTF-16), due to combining characters.
- ygra 13y agoUTF-16 is just as bad as UTF-8 regarding variable-width code points. The only thing you always have (unless using compression schemes like SCSU) is random access to code units. Only UTF-32 also allows random access to code points. However, that's still of questionable value because when dealing with text you often want to handle graphemes, not code points, code units or bytes.
- jmdavis 13y agoUTF16 is variable length just like UTF8 is (so don't assume 2 bytes == 1 character).
- jeltz 13y agoFor this you do not need to reverse the string in a unicode aware way. You need to operate on the raw bytes.
- ygra 13y agoSubstrings exhibit similar problems and those are used quite often. It's just that in this case the effect of seeing it fail is a little more dramatic (i.e., l̈ – which doesn't even seem to render properly here).
- jeltz 13y ago"l̈" renders just fine for me, maybe your font does not include it.
- ygra 13y agoVerdana doesn't seem to properly support U+0308, apparently. It's wrong (with that font) in Chrome, IE 10, Firefox and Word 2010. Other operating systems might substitute a different font that works better, perhaps.
- jeltz 13y agoYes, I am running Debian without having installed the Microsoft core fonts so Verdana is substituted for DejaVu Sans.
- abcd_f 13y agoMay be not reversing, but trimming a Unicode string to certain character count is a close relative and it is a very common operation.
- lmm 13y agoRight, but what's the count you want there? It's either a byte count or a grapheme cluster count. The .count() on most current languages' string types doesn't correspond to either of those, so isn't really useful.
- jeltz 13y agoWhat do you use it for? Unless you have a monospaced font the number of characters do not mean much. So unless you are implementing command line tools or text editors it should not be that common.
- ygra 13y agoEllipsisising text when it does not fit into a label, for example. And if you just remove code points from the end (instead of graphemes) until the string (including ellipsis) fits then you might just drop a diacritic.
- Pxtl 13y agoTruncating with ellipsis in the GUI in a desktop app. I can measure rendered length on a desktop, so I can truncate down to the desired number of pixels, round down to the nearest char, and then tack on "...". I would hate to see a semantically-important accent mark lost this way.
- ashashwat 13y agoYou have a search query and you want to remove stopwords and normalize the query.
- jerf 13y agoI have a database field limited to 100 "characters" [1]. The user sent me a form submission with 150. I need to do something to resolve that. This is incredibly common. Truncation to a defined size is routine. [1]: I'm leaving "characters" undefined here, because no matter what Unicode-aware definition you apply here, you've got trouble.
- chiph 13y agoI've been waiting for someone to ask me to reverse a string in an interview, so I can tell them why the code I just wrote for them (using the XOR trick, which is what they're usually expecting), is wrong.
- zwily 13y agoWhen I've asked people to reverse a char* in the past, it's just been to see if they understand the basics of pointers. The XOR "trick" hasn't been impressive since high school. :)