Concepts / Introduction to Text Manipulation

Introduction to Text Manipulation

The string type exists so that you do not have to work directly with raw character arrays and pointers when you manipulate text.

  • Programming

Why Programs Need to Handle Text

Text manipulation is a major part of most programs. Almost any program that does something useful with people or data has to deal with words, names, lines, and messages at some point. Whether a program reads a name, searches a document, or rearranges words, it has to store text and change it in useful ways.

Because this need shows up so often, C++ provides tools for working with text. This article introduces those basic tools so that later work with specific text operations has a clear foundation. The aim here is to build a mental picture of the pieces first: where text is stored, what helps you change it, and what helps you find things inside it.

EASY

Think of three ordinary tasks a program might do that involve text, such as asking a user for a name or printing a report. For each one, say whether the program must store the text, change the text, or search inside it.

Hints
  • Consider what the program has to remember after the user types something.
  • Searching means looking for a piece of text inside a larger piece of text.

Tracing a Piece of Text Through a Program

To see why these tools matter, follow one piece of text through a small task. Suppose a program asks a user for a full name, stores it, checks whether it contains a particular word, and then rearranges the words before printing them. Each step depends on the text being held in a form the program can work with.

Worked example

Handling a name from input to output

A program reads the text Ada Lovelace from the user, checks whether it contains the word Lovelace, and then prints the words in reverse order.

  1. 1
    Store the text

    The program must hold the characters Ada Lovelace somewhere so it can be used later. This is the first thing text manipulation requires: the text must be kept in a value the program owns.

  2. 2
    Locate a piece of the text

    The program checks whether the word Lovelace appears inside the stored text. This is a search, which is the kind of job regular expression matching is used for when the thing being looked for can be described as a pattern.

  3. 3
    Change the arrangement

    The program rearranges the two words so the output reads Lovelace Ada. Rearranging text is a change to the stored characters, which is why the program needs a way to hold and handle text rather than only read it.

  4. 4
    Output the result

    The rearranged text is printed. The program has now used the same piece of text for storing, searching, changing, and displaying.

Result

The program prints Lovelace Ada after storing, searching, and rearranging one piece of text.

Notice that the example did not need any special knowledge of how characters sit in memory. It needed a way to hold the text, a way to search for part of it, and a way to change it. The rest of the article looks at the C++ tools that supply these abilities.

The string Type Instead of Raw Character Arrays

The string type is part of the C++ standard library, which means it is provided for you rather than something you must build yourself. Its main purpose is to simplify text manipulation. Instead of working with raw character arrays and the pointers that move through them, you work with a string object that the library provides.

The source contrasts two ways of handling text. The first is C-style manipulation of arrays of characters through pointers. The second is the string type from the standard library. The table below summarizes that contrast.

ApproachHow the text is handledWho does the bookkeeping
C-style character arraysText is handled as arrays of characters, moved through with pointersThe programmer manages the characters and positions directly
Standard library string typeText is held in a string object provided by the libraryThe library provides the object, which simplifies text manipulation

Two ways of handling text, as contrasted in the source

Key point

The string type exists so that you do not have to work directly with raw character arrays and pointers when you manipulate text.

Finding Patterns with Regular Expression Matching

Where the string type helps you hold and handle text, regular expression matching helps you locate things inside it. The idea is to describe a pattern and then find where that pattern appears in a piece of text. You give the matcher a pattern and a text, and the matching process reports whether the pattern appears and where.

Patterndescribes what to findRegex matchpattern is compared againsttextMatch found?yes or noNo matchTextthe stored text to searchMatched partswhere the pattern appearspattern intext incomparenoyes
What happens to a pattern and a text when a regex match runs, and what the program gets back: a match or no match, and which parts matched.

Read the diagram from top to bottom. The pattern and the text both go into the matching step. The result is either a match or no match. When a match is found, the program knows which parts of the text the pattern covered. This is the basic workflow behind searching text by description rather than by an exact copy of the characters.

The source notes that C++ provides regular expressions in a form similar to what is common in most modern languages. A programmer who has used regular expressions in another language will therefore find the core idea familiar, even though the details of C++ syntax are covered in later work.

Why Character Types Matter

The source states that both string and regex objects can use a variety of character types, and it gives Unicode as one example. This means the text handled by these objects is not restricted to one narrow set of characters. The character type is a choice that these objects can support in more than one form.

String and regexobjectsone family, many charactertypescharwchar_tchar16_tchar32_tcharacter typecharacter typecharacter typecharacter type
How one family of string and regex objects covers several character widths, and how the character types named in this chapter relate to one another.

The diagram shows the central idea: a single family of string and regex objects can be used with more than one character type. The choice of character type is part of how the text is represented. The chapter introduces these types at the level of the big picture, so the important point for now is that the same kinds of text tools work across several character types rather than only one.

Mistakes Beginners Make with Text Tools

  • Common mistake

    Thinking that manipulating text means working with raw character arrays and pointers by default

    Why it fails

    The string type exists so that you do not need to work directly with raw character arrays and the pointers that move through them.

    Do this instead

    Start from the string type that the standard library provides, and treat C-style character arrays as the older approach the source contrasts it with.

  • Common mistake

    Treating regular expression matching as a way to copy text

    Why it fails

    Regular expression matching is used to locate things inside text by describing a pattern and finding where it appears.

    Do this instead

    Use matching to find where a pattern appears, then use string operations to hold or change the text that was found.

  • Common mistake

    Assuming text is always limited to one narrow set of characters

    Why it fails

    The source states that string and regex objects can use a variety of character types, including Unicode.

    Do this instead

    Keep in mind that the character type is a choice these objects can support in more than one form.

  • Common mistake

    Ignoring that text handling is needed in most programs and waiting until a late chapter to think about it

Practice Tracing Text Tasks

MEDIUM

A program reads a sentence from a user, finds every place where a number appears in it, and then prints the sentence with the numbers removed. Describe which parts of this task are storing text, which parts are finding text by pattern, and which parts are changing text. Then say which tool from this article each part relies on.

Hints
  • Storing the sentence is about holding text, which the string type supports.
  • Finding every number is about describing a pattern and locating where it appears, which is regular expression matching.
  • Removing the numbers changes the stored text, so it is a manipulation step.

Key Takeaways

  • Text manipulation is a major part of most programs, because programs must store text and change it in useful ways.
  • The C++ standard library string type simplifies text work by providing a string object, so you do not have to work directly with raw character arrays and pointers.
  • Regular expression matching helps locate things inside text by describing a pattern and finding where that pattern appears, in a form similar to most modern languages.
  • String and regex objects can use a variety of character types, including Unicode, so the character type is a choice these objects can support in more than one form.

Related concepts

More concepts starting with I