I’ve noticed a lot of programming introductions (almost any language) usually include a heavy barrage of string manipulation quite early, such as:
- Count the number of “xx” in the given string. We’ll say that overlapping is allowed, so “xxx” contains 2 “xx”.
- Reverse the given string and remove all vowels from it.
Not just online courses, but even in degree programmes.
But the thing is, I’ve never encountered a situation in “real life” where you had to do anything like that. Is it just luck, or do you really need to do heavy manipulation in the above manner in some fields of programming? Can you give a description or an example where it is needed?
Edit:
Just to clarify, not necessarily string processing itself as in echoing variables to a html template, or other basic stuff like that, but the more arcane type of processing, like reading every second character? But yes, perhaps it is just for training purposes instead of direct needs. The file format conversion is a good note, though.
5
Real life programming will almost certainly involve string manipulation at one point or another. Your example is a very specific case and may or may not appear in a real life scenario (In reality, I’m sure somebody, somewhere has had to tackle that problem in real life).
However, the real life applicability of a particular use case is not necessarily the point of those exercises when introducing a new language or teaching someone brand new to programming. The point of those exercises are likely to be any of the following (depending on context):
- To teach a programmer the basic syntax of a given language using an easily understood problem.
- To provide an exercise allowing novice programmers to learn basic programming logic without having to understand a complex problem.
- To express a languages ability to solve a particular string manipulation problem.
1
Text files and text data are everywhere.
Converting .csv
to .xml
, reading .py
for execution or .f90
for compilation, building and sending spam from a list of mail addresses, building a database of keywords for indexing the web: all of that is string manipulation.
Do computers do something else than manipulating strings?
3
String manipulation actually is fairly common when you’re doing I/O, which includes most network programming. Unicode complicates things, of course, but text remains the closest thing we have to a universal interface.
But where it really shines is as a metaphor. Working with individual characters in strings is, in many ways, not unlike working with cells in memory. At the lowest levels, the operations can be almost identical. This makes strings a less abstract way of learning the very basics of working with data at low levels, and that makes it valuable to learn early.
These are very specific examples, really just given as training for string manipulation.
And it is more common than you think – mostly in parsing and UI code.
For example – parsing dates out of strings – this requires extensive string manipulation.
Or parsing HTML into a DOM – same kind of thing.
You will also need to manipulate strings for UI purposes – constructing strings based on specific data and conditions.
Is it just luck, or do you really need to do heavy manipulation in the above manner in some fields of programming?
There are domains where string manipulation is not a necessity, or it is a marginal problem. There are also domains where it is a critical requirement.
For example, web technologies depend critically on text manipulation (web and mail servers and clients); so do most software development tools (IDEs, editors, compilers, etc).