Welcome to the readings on characters, string and Texts in Chapter 3 of Skansholm. This is the 9th topic covered in CS201 2008.
An important part of any programming language is being able to handle character data. So far you have only met ways to manipulate numbers and Booleans. In this chapter you learn about how characters are handled in C++. This includes single characters, strings of characters, and the older C texts.
A single character in a C++ program appears inside a pair of single quotes. These days ASCII is typically used to encode characters. The book, on page 81 shows you some special characters such as a single quotation mark with the backslash character. Page 82 lists some of the common "escape codes". You don't have to worry in CS201 about using Octal and Hexadecimal code.
You can store a single character in a variable of type char. And in section 3.2 the book shows a couple of programs that read and write characters. On page 83 is a program that copies a file followed over the page by a useful list of ways of handling characters on input.
In section 3.3 the text introduces the C++ way of handling strings of characters. This in a special library called string.
Notice the difference between "cin >>s;" which skips white space, and "cin.getline(s)" which does not.
Page 87 shows you how to reserve storage for a string. Pages 88 through to 92 gives examples of several ways of slicing, dicing and combining strings. The most useful function are: using "+" to concatenate strings, size to get the number of characters, find to see where a substring is, and using substr to get at parts of a string. Page 93 and 94 is a pretty complete list of things you can do with strings. However it has many extra minus signs in it.
You should skip from page94 to page 34 unless you will be working outside the USA.
Section 3.4 goes over the way C handle strings as null-terminate char arrays. The input and output of fixed length fields at the bottom of page 101 may be useful. The old str library is dangerous so I hope we will be able skip pages 102 to 107.
Here ends the readings for class number 9.
3.1 Characters in C++
Page 81 -- octal and hex -- not in CS201.
pages 81-82 iodos
In CS201 you can ignore non LATIN-1 characters and Skanholm`s
special libraries for handling other codes. They will not
be part of CS201.
Page 82 -- Escape sequences -- a handy table
3.2 Storing reading writing characters
Reserving storage large enough for a single character
char variable;
Getting the next character
cin>>variable;(skips spaces and tabs and end-of-lines)
cin.get(variable)(better!)
Output
cout << variable;
Page 83 -- Copying a file
In Unix there is a program that does everything that this program
does and more. It is called cat so I call this kitten.
[ kitten.cpp ]
Notice how we can input ANY file into a program like this
program < file
Pages 84-85 list some other useful functions for handling input and out characters.
3.3 string
You can do many things to strings: declare, input, output, initialize,
copy, concatenate (with +), append, erase, insert, replace, find the size,
find a substring, ... Just like VB with different syntax.
page 93-94 Table of string operations
Note. In some editions there is an extra hyphen in many of the
example operations.
Note. I don't expect you to memorize all of these operations! First, put a post-it note on this page so you can find it quickly when programming projects.
But put notes on the following onto your cheat sheet for use in the labs, quizzes, and the final:
The rest are likely to be useful after CS201 rather than during it.
Dont study pages 95-96 -- alpha
pages 97-98 Vectors of strings
3.4 Character Arrays
Figure 3.1 How a text string is stored
Notice the extra character
'\0'at the end of the array. This is a character that is not easily input and has no effect when output and so C/C++ figures that it can be used to single the end of a piece of text.
If part of the algorithm reads
If A is true then do B while C is true else do D.Then you'll probably have
if(A)
{
while(B)
{
C
}
}
else
{
D
}Notice the careful indenting to make the structure obvious.
Another classic part of an algorithm puts the if inside the while:
Repeat the following until A is false
If B is true then do C else do Dbecomes
while(A)
{
if(B)
{
C
}
else
{
D
}
}
To summarize: first get an algorithm, second code it with indenting.
How do I incorporate whiles and ifs with messing up
First: get an algorithm. Second: code it with indenting.
How do you know when to use a string
Use a string any time you have data that has characters in it:
names, addresses, StudentIds, SSNs, Dates, Times, .....
Basically they can be used in just about any program.
Any chance of going over the stuff in todays quiz
Did that last class already!
What is the difference between standard string and class alpha
You must learn about string but not about alpha.
Describe in further detail about getting fast and easy access to elements in the sequence of a vector
If you have a vector with 1,100 elements:
vector <double>example(1100);Then you can access the first element as
example[0]The 115th item
example[114]The last item
example.last()
More you can access a whole subset
for(int i=114; i<120; i++)
{ do_something with example[i]
}
1101101You interpret these as a number by using powers of 2:
64+32+ 8+4+ 1
There are some standard algorithm for converting decimal to binary and back. Each computer Scientists has their own gimmicks --- A friend taught me how you can do the conversions on a Japaneses Soroban abacus!
I can show you a program that uses characters and strings to write out the binary form of a number if you like: [ binary.cpp ]
Can you create a while statement that will read the a program backwards
Well, programs are in files and we can write code that reads
a file in the order in which it is stored.... but
we don't have the tools (in CS201) to go backwards
thru a file.
What I can demonstrate is how you can read a file forward and put it in a string backwards and then print it out: [ backwards.cpp ] , you can download this and compile it into an executable called backwards and then try this command
./backwards < backwards.cpp
How will we use strings
I'm not sure what I'm going to ask you to do.... except it
will involve: substr, find, +, [...], and a few other simple
operations.
There is no knowing what you might choose to do in your project with
strings.
What is the function of a string
To hold character data.
What is a character array
It is a piece of computer storage divided into character sized pieces
and numbered from 0, 1, 2, .... up to the size - 1.
In other words it is an array of chars.
Think of a character array as a box in a form where you can write
or type things. Fixed size. Type in what you want, where
you want, and read it out again, and change the content.
Why is the old str library dangerous
It does no error or bounds checking. Even the simplest operation of
copying can crash the program or operating system if not
written very carefully.
Explain more about strings
See below!
What is a simple explanation of the standard class string
"Standard" means that you should get the same behavior from the
<string> library where ever you use C++. The word class
means that strings are objects and we can do things them like
this
object.operation( data ).
A string, in theory, has these operations:
| Theory | C++
|
|---|
Arrays have a fixed size and this makes them hard to use. Strings automatically get the storage they need when they grow. Automation makes strings easier to use.
Please use strings when ever you can!
Is there a limit to the number of characters in a string
Not really. I think you can fill up the whole of the virtual
memory given to you program in a string -- 1 byte to a character.
Should we constantly update the algorithm and comments in our projects
In this class.... you get more points that way.
Outside ... you will make live easier for you and your team mates.
Is there are better way to get credit than asking a question
You get 1 point for a signed blank sheet -- it says you were here.
You can make up 5 points if you attend a CSE Dept Seminar (See [ seminar/ ] ) and give me a one paragraph essay as a summary.
I, personally, would prefer to ask a question.
(Note: when the text has good review questions then I set these
as the homework. But this book doesn't have them
)
What is MS-DOS
DOS stands for "Disk Operating System" and MS-DOS was Microsoft's
first big contract -- to supply a Floppy Disk-based Operating
System for the new IBM Personal Computer.
When you open the Command Line/ Run Command box in Windows you can input a MS-DOS command.
. . . . . . . . . ( end of section Questions) <<Contents | End>>
Quiz 4 on arrays
Lab 05 on arrays vectors strings