Arrays and Strings - Lecture Notes | CS 145, Study notes of Computer Science

Material Type: Notes; Professor: Ehlmann; Class: Introduction to Computing for Engineers; Subject: Computer Science; University: Southern Illinois University Edwardsville; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-d8c-1
koofers-user-d8c-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Notes—Chap. 7c Arrays and Strings (pp. 297 – 303,
and Appendix D)
- This section introduces the defined type string.
- Can work with character strings in C++ in two ways:
- as Cstrings, i.e., character arrays
- as objects of the class string, whose definition is provided when you
#include <string>
- Quoted strings in C++ are represented as an array of characters terminated
by a null character, i.e., \0, which is the byte containing all 0 bits.
- Example: "ABC" is stored as 4041420016
- A "Cstring variable" named s1 can be declared in any of the following ways:
char s1[] = "ABC";
char s1[4] = "ABC"
char s1* = "ABC";
- Functions are available in the cstring library to manipulate Cstrings, e.g.,
char s2[10];
...
strcpy(s2, s1); // copies s1 into s2
- s2 is now: 40414200????????????16
- Using Cstrings is the more primitive way of handling strings; using objects of
type string is better (and is so done by the textbook).
- Example:
string s1 = "ABC";
string s2;
...
s2 = s1; // copies s1 into s2
- See Appendix D for string constructors, functions, and operators.
pf3

Partial preview of the text

Download Arrays and Strings - Lecture Notes | CS 145 and more Study notes Computer Science in PDF only on Docsity!

Notes—Chap. 7c Arrays and Strings (pp. 297 – 303,

and Appendix D)

- This section introduces the defined type string.

- Can work with character strings in C++ in two ways:

- as Cstrings , i.e., character arrays

- as objects of the class string, whose definition is provided when you

#include

- Quoted strings in C++ are represented as an array of characters terminated

by a null character , i.e., \0, which is the byte containing all 0 bits.

- Example: "ABC" is stored as 4041420016

- A "Cstring variable" named s1 can be declared in any of the following ways:

char s1[] = "ABC";

char s1[4] = "ABC"

char s1* = "ABC";

- Functions are available in the cstring library to manipulate Cstrings, e.g.,

char s2[10];

strcpy(s2, s1); // copies s1 into s

- s2 is now: 40414200???????????? 16

- Using Cstrings is the more primitive way of handling strings; using objects of

type string is better (and is so done by the textbook).

- Example:

string s1 = "ABC";

string s2;

s2 = s1; // copies s1 into s

- See Appendix D for string constructors, functions, and operators.

  • Example of program that uses strings: (Fig. 7.16, p. 301, slightly revised) // Processes a file in which each line consists of a name, birth date, and // address, with a newline character immediately following the address: // LastName, FirstName MiddleName Mo/Day/Yr Address // Displays the information of each record in a sentence. #include #include #include using namespace std; const string MONTHS[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int main() { string filename, // name of input file containing data first, // first name last, // last name middle, // middle name addressPt, // address part address; // acculumated address int birthMon, // birthday month birthDay, // birthday day birthYr; // birthday year char nextChar; // next character in input stream cout << "Enter name of input file=> "; cin >> filename; ifstream infile(filename.c_str(), ios::in); getline(infile, last, ','); // input comma-terminated name while( !infile.fail() ) { infile >> first; infile >> middle; // Input slash-separated date, e.g, 10/31/1949. infile >> birthMon >> nextChar >> birthDay >> nextChar >> birthYr; // Build rest of line, separating pieces by single blanks and // stopping at newline character. infile >> addressPt; address = addressPt; infile.get(nextChar); while (!infile.fail() && nextChar != '\n'){