Lecture 11 - Taking Strings Apart

Lecture 11 - Taking Strings Apart - Taking Strings Apart...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Taking Strings Apart (and putting them together) Lecture 11 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser /fall_2011/csc1254.html Programming Principle of the Day Write Code for the Maintainer - Almost any code that is worth writing is worth maintaining in the future, either by you or by someone else. The future you who has to maintain code often remembers as much of the code, as a complete stranger, so you might as well always write for someone else. A memorable way to remember this is Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live. 10/4/2 011, Lecture 11 CSC 1254, Fall 2011, Taking Strings Apart 2 Abstract This lecture will go back to our initial example of framing a string. So far, we have dealt with strings as a whole only. In this lecture we will look at the characters of a string separately: strings are apparently special containers. Many techniques we know from vectors are applicable to strings as well. 10/4/2 011, Lecture 11 CSC 1254, Fall 2011, Taking Strings Apart 3 Splitting a Line into Words Well write a function which takes a whole line of input and returns a vector of strings holding the single words of that line: vector<string> split( const string& s); Strings support indexing in the same way as vectors: s[0]: refers to the first character in the string s s[s.size()-1]: refers to the last characters in a string 10/4/2 011, Lecture 11 CSC 1254, Fall 2011, Taking Strings Apart 4 Splitting a Line into Words This looks like: Words are split at whitespace characters Very similar to the processing during stream input into a string 10/4/2 011, Lecture 11 CSC 1254, Fall 2011, Taking Strings Apart 5 Some text read into a line i j Splitting a Line into Words vector<string> split( const string& s) { vector<string> words; typedef string::size_type size_type; size_type i = 0; // invariant: we have processed characters [original value of i, i) while (i != s.size()) { // ignore leading blanks, find begin of word // find end of next word 10/4/2 011, Lecture 11 CSC 1254, Fall 2011, Taking Strings Apart 6 Splitting a Line into Words Ignore leading blanks // invariant: characters in range [original i, current i) // are all spaces while...
View Full Document

Page1 / 29

Lecture 11 - Taking Strings Apart - Taking Strings Apart...

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