Working With Text - C++

Before the advent of graphics, text was the basis of all programs. Text-based adventure games and text-based Bulletin Board Systems were our first programming experiences. Text is still a very important element of programming. In this section, you find out how to assemble and store text. Text creates the simplest medium for displaying the output of programs. This book starts by using only text because text is sufficient to display the output of most programs.

The technical name for text in the computer world is string. For example, in the “Hello World” program, “Hello World” is a string.

Assembling Strings

A string is a collection of characters; in general, we think of a character as a symbol that you can type from the keyboard, including spaces. The computer interprets strings such as “For Honor!!!” as a series of characters, each occupying one letter or space(‘F’ is the first character,‘o’ is the second, and so on). Strings are enclosed within quotation marks, but characters are enclosed within single quotes. For example, “a”, “ “ (space), “4”, and “%” are all strings consisting of one character. However, ‘a’, ‘ ’, ‘4’ and ‘%’ are all characters. You can use characters almost anywhere that you can use a string.

However, characters are not always what you might expect. You cannot type some characters from the keyboard. There are 256 different kinds of characters.

You can access many other characters by holding down Alt and writing their numeric values. For example, the numeric value of A is 65. Appendix C, “Using the Standard ASCII Character Table,” provides a reference for the standardized characters and their numeric values. It is important to realize the distinction between strings and characters. Strings are made up of characters, but are quite different from them. In addition, a character can easily be converted to a string, but a character is not a string.
This might all seem complex, but creating strings is actually easy to do. The following are some examples of strings. As you can see, they are just a little bit of text surrounded by quotation marks.

Storing Strings
You can store strings in a computer so that you don’t have to write them more than once. Specifically, you store strings by putting them in memory (we cover memory in detail in Chapter 2, “Descending Deeper . . . into Variables”). The code for strings is held in a library aptly named the string library. In order to store strings, you must include the string library. You do this by including the following line at the beginning of your program:

#include <string>

When you want to store a string, you must provide a name for it so that the computer knows which string you want to access in the future. For example, we will put “A dragon is coming” into memory and name the string yell.

There is one small complication, though. A computer can store many things in memory, not just strings, and it stores different things differently. For example, a number is stored differently than a string is stored. So, you must also tell the computer what kind of thing it is storing. Here’s what you must provide the computer:

  • The type of thing that is being stored (in this case, a string)
  • The characters that make up the string
  • The name of the string

Although C++ does not provide you with the ability to store strings, the standard library does. In order to use it, though, you must include the <string> library in your program.

Here is how you store a string, after you include the library:

string yell = "A dragon is coming";

First comes the type of thing being stored: a string. Second comes the name of the string: yell. The equal sign tells the computer that the string, “A dragon is coming”, is being stored in yell. Last comes the semicolon, which tells the computer that you have finished the command and are moving on to another command.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

C++ Topics