strings - Programming Strings P102 Prog. Fundamentals:...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming Strings
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MP102 Prog. Fundamentals: Strings / Slide 2 Character Strings A sequence of characters is often referred to as a character “string”. A string is stored in an array of type char ending with the null character '\0 '.
Background image of page 2
MP102 Prog. Fundamentals: Strings / Slide 3 A string containing a single character takes up 2 bytes of storage. Character Strings
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MP102 Prog. Fundamentals: Strings / Slide 4 Character Strings
Background image of page 4
MP102 Prog. Fundamentals: Strings / Slide 5 Character Strings
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MP102 Prog. Fundamentals: Strings / Slide 6 Character vs. String A string constant is a sequence of characters enclosed in double quotes. For example, the character string: char s1[2]= " a "; //Takes two bytes of storage. s1: On the other hand, the character, in single quotes: char s2= `a`; //Takes only one byte of storage. s2: a \0 a
Background image of page 6
MP102 Prog. Fundamentals: Strings / Slide 7 Character vs. String
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MP102 Prog. Fundamentals: Strings / Slide 8 Example 1 char message1[12] = "Hello world"; cout << message1 << endl; message1: char message2[12]; cin >> message2; // type "Hello" as input message2: H e l l o w o r l d \0 H e l l o \0 ? ? ? ? ? ?
Background image of page 8
MP102 Prog. Fundamentals: Strings / Slide 9 Example 2: String I/O String can be input using the extraction operator >> , but one or more white spaces indicates the end of an input string. char A_string[80], E_string[80]; cout << "Enter some words in a string:\n"; cin >> A_string >> E_string; cout << A_string << E_string << “\nEND OF OUTPUT\n"; Output: Enter some words in a string: This is a test. Thisis END OF OUTPUT
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
P102 Prog. Fundamentals: Strings / Slide 10 getline The function getline can be used to read an entire line of input into a string variable. The
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/30/2010 for the course COMP COMP102 taught by Professor Lrr during the Fall '09 term at HKUST.

Page1 / 27

strings - Programming Strings P102 Prog. Fundamentals:...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online