[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 09
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] <09> [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Podcasts: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Labs: [01] [02] [03] [04] [05] [06] [07] [09] [10]
Thu Feb 7 09:20:24 PST 2008

Contents


    Chapter 3: pages 79-108 Characters and Strings

      Project 3 Due

      Reading

        Introduction to Characters Strings and texts

        Here [ 09.mp3 ] is a podcast introducing the reading. The text form follows.

        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:

      1. +, substr, getline, clear, size, find

        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.

        page 101 -- inputing arrays of characters

        page 104-108 -- not in CS201

        These are old fashioned and dangerous to use. I do not expect you to know about them in quizzes and the final. I hope you won't need to use them in your project work in CS201. You may need them at some point in your career, however.

      Glossary

      Syntax

    1. character_literal::= quote character quote. A single quote means a single character.
    2. hex::="A way to code numbers using base 16 -- digits are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F".
    3. octal::="A way of coding numbers with base 8 rather than 10".
    4. string_literal::= quotes character quotes. Double quotes means any number of characters.

      Questions

        What are the limits of error messages

        The can spot syntax errors and warn you of one or two logic or execution errors.

        How to combine if and while statements

        It depends on the algorithm you are trying to code.... when you have more than two or three ifs and whiles you need to first sort out your algorithm.... your plan of how to solve the problem.

        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 D
        becomes
         	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]
         	}

        How to character literals work

        The compiler spits the blip (') on the input and enters the code for translating character literals. This code puts the right character or pattern of bits into a one byte storage area:
        1. If the next character is a blip(') then report a compile error.
          If the next character is not a backslash (\) then that is the character to store. If the character after that is not a blip(') report compile error and exit. But if it is... store the character and exit.
        2. If the next two characters are a backslash followed by a 0 then translate the string from octal to binary.
        3. If the next three characters are a backslash followed by an x and a 0 then translate the string from hexadecimal to binary.
        4. Else compile error.

        (Note 1 -- I let you work out the details). (Note 2 -- I'm guessing about the algorithm).

        Can you teach us to write in binary

        Yeah -- easy. Just pick a few binary digits -- each one either a '1' or a '0'...
         			1101101
        You 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:
        TheoryC++
      1. Concatenation s1+s2
      2. Substring s.substr(start, size)
      3. Find matching substring s.find(match)
      4. Find number of characters in string s.size()
      5. Get the i'th character s[i]
      6. How do strings differ from character arrays

        Strings have a lot of useful operators that arrays don't have.

        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

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. TBA::="To Be Announced", something I should do.
  4. TBD::="To Be Done", something you have to do.
  5. UML::="Unified Modeling Language", [ uml.html ] (beginner's introduction to the UML).

End