Lecture 2 - Strings

Lecture 2 - Strings - Working with Strings Lecture 2...

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

View Full Document Right Arrow Icon
Working with Strings Lecture 2 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/˜hkaiser/fall_2011/csc1254.html
Background image of page 1

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

View Full DocumentRight Arrow Icon
Abstract This lecture will look at strings. What are strings? How can we input/output strings? How can we manipulate strings? What functionality is provided by the std::string type? 8/25/2011, Lecture 2 CSC1254, Fall 2011, Strings 2
Background image of page 2
C++ Libraries Groups related operations ▫ Header file provides function prototypes and usage comments ▫ Compiled library contains implementation C++ Standard Libraries ▫ E.g. string, iostream, fstream ▫ #include <foo> ▫ Terse lowercase names: cout, getline, substr 8/25/2011, Lecture 2 CSC1254, Fall 2011, Strings 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
String Input Given the following interaction: Please enter your name: John We would like to print: Hello John! 8/25/2011, Lecture 2 CSC1254, Fall 2011, Strings 4
Background image of page 4
String Input // Ask a persons name, greet the person #include <iostream> #include <string> int main() { // ask for the persons name std::cout << "Please enter your first name: " ; // read the name std::string first_name; // define 'first_name' std::cin >> first_name; // read into 'first_name' // write a greating std::cout << "Hello, " << first_name << std::endl; return 0; } 8/25/2011, Lecture 2 CSC1254, Fall 2011, Strings 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
String Input Place to put input into: variable ▫ Variables are objects which have a name ( first_name ) ▫ Objects are part of the computer memory with an associated type ( std::string ) std::string ▫ Part of namespace std #include <string> 8/25/2011, Lecture 2 CSC1254, Fall 2011, Strings 6
Background image of page 6
String Input Variable definition: std::string name; ▫ Local variable, is destroyed at end of block ▫ Type defines the available operations and interface exposed by the object Initialization, here: empty (or null) string String input using operator >> from std::cin ▫ Discards leading whitespace ▫ Stops at whitespace Input/output buffering occurs ▫ flushing is performed: buffer full, input, explicit request 8/25/2011, Lecture 2 CSC1254, Fall 2011, Strings 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

Lecture 2 - Strings - Working with Strings Lecture 2...

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

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