Taking Strings Apart (and putting them together) Lecture 11 Hartmut Kaiser

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Spreadsheet Vocabulary
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Excel 2007 ® Business and Personal Finances How can Excel 2007 help you format a workbook?
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
PowerPoint: Tables Computer Information Technology Section 5-11 Some text and examples used with permission from: Note: We are.
 You've probably come into contact with web sites in which the browser window seemingly allowed you to move around between several different pages. 
The Stack and Queue Types Lecture 10 Hartmut Kaiser
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
Organizing Programs and Data 2 Lecture 7 Hartmut Kaiser
Input & Output: Console
Lists in Python.
Working with Strings Lecture 2 Hartmut Kaiser
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Chapter 8 Arrays and Strings
Creating your first C++ program
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 7 – Adding and.
Defining New Types Lecture 21 Hartmut Kaiser
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Working with Batches of Data Lecture 4 Hartmut Kaiser
Looping and Counting Lecture 3 Hartmut Kaiser
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Using Library Algorithms Lectures 12 and 13 Hartmut Kaiser
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Using Associative Containers Lecture 18 Hartmut Kaiser
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Writing Generic Functions Lecture 20 Hartmut Kaiser
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
String Class Mohamed Shehata 1020: Introduction to Programming.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Using Sequential Containers Lecture 8 Hartmut Kaiser
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and.
A Sample Program #include using namespace std; int main(void) { cout
Library Functions. CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Chapter 5 Using Sequential Containers and Analyzing Strings.
Bill Tucker Austin Community College COSC 1315
Strings CSCI 112: Programming in C.
CSS Layouts: Grouping Elements
Fundamentals of Characters and Strings
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Containers and Lists CIS 40 – Introduction to Programming in Python
Working with Strings Lecture 2 Hartmut Kaiser
PowerPoint: Tables and Charts
Introduction to C++ Programming
Working with Strings Lecture 2 Hartmut Kaiser
Defining New Types Lecture 22 Hartmut Kaiser
CSc 110, Spring 2018 Lecture 7: input and Constants
A First Program.
Using Library Algorithms
EECE.2160 ECE Application Programming
Using string type variables
Std Library of C++.
Python Strings.
Programming Strings.
An Introduction to STL.
getline() function with companion ignore()
Introduction to Computer Science
Presentation transcript:

Taking Strings Apart (and putting them together) Lecture 11 Hartmut Kaiser

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.” 3/3/2015, Lecture 11 CSC 1254, Spring 2015, 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. 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 3

Splitting a Line into Words We’ll write a function which takes a whole line of input and returns a vector of strings holding the single words of that line: vector split(string const& 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 character in a string Our function will find indices ‘i’ and ‘j’ delimiting each of the words, where the range of characters [i, j) constitutes the word 3/3/2015, Lecture 11 CSC 1254, Spring 2015, 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 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 5 Some text read into a line ij

Splitting a Line into Words vector split(const string& s) { vector words; vector ::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 // if we found some non-whitespace characters, store the word } return words; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 6

Splitting a Line into Words Ignore leading blanks // invariant: characters in range [original i, current i) // are all spaces while (i != s.size() && isspace(s[i])) // short-circuiting ++i; Find end of next word // find end of next word auto j = i; // invariant: none of the characters in range // [original j, current j) is a space while (j != s.size() && !isspace(s[j])) // short-circuiting ++j; 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 7

Splitting a Line into Words Store the word if any // if we found some non-whitespace characters if (i != j) { // copy from s starting at i and having j – i characters words.push_back(s.substr(i, j - i)); i = j; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 8

Test our new Function Read a line of input and invoke our function Write content of returned vector Results should be the same as when using an input stream to read strings  Remember, this separates words as well Run both tests on same input data and compare results 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 9

Test our new Function Simple test program (C++11, gcc 4.6): int main() { string in; // read and split each line of input while (getline(cin, in)) { vector v = split(in); // write each word in v for (auto const& s: v) cout << s << endl; } return 0; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 10

Test our new Function Test word splitting using input streams: int main() { string s; while (cin >> s) cout << s << endl; return 0; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 11

Alternative Implementation int main() { string input; while (getline(cin, input)) { stringstream sstrm = input; vector v; string str; while (sstrm >> str) v.push_back(str); for_each(v.begin(), v.end(), [](string const& s) { cout << s << endl; }); } return 0; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 12

Putting Strings Together Earlier we wrote a program to frame a string  Never created the output in a string  Rather printed the parts separately Let’s assume vector is a ‘picture’ (ASCII art), each string is one line Now, we will build a program framing such a picture  Puts together the whole picture in a data structure before printing it 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 13

Putting Strings Together Write all strings in a vector on one line each and surround it by a border For example, a vector holding “this is an”, “example”, “to illustrate”, “framing” will result in: ***************** * this is an * * example * * to illustrate * * framing * ***************** 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 14

Putting Strings Together Box is rectangular not ragged as single strings Find the longest string string::size_type width(vector const& v) { string::size_type maxlen = 0; for (auto const& s: v) maxlen = std::max(maxlen, s.size()); return maxlen; } Exercise: use standard algorithm (accumulate) 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 15

Framing Pictures What should the interface look like?  Let’s consider vector to represent a ‘picture’  Function frame() will take a ‘picture’ and should return a new ‘picture’: vector frame(vector const& v) { // find longest string // create top line and append to result // append each line from v to result after adding '*' // create bottom line and append to result } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 16

Framing Pictures vector frame(vector const& v) { vector ret; auto maxlen = width(v); // find longest string // create top line and append to result string border(maxlen + 4, '*'); ret.push_back(border); // append each line from v to result after adding '*' for (auto const& s: v) { ret.push_back( "* " + s + string(maxlen - s.size(), ' ') + " *"); } ret.push_back(border); // write the bottom border return ret; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 17

Vertical Concatenation What else can we do to ‘pictures’?  Concatenation! Vertical and horizontal ‘Pictures’ are represented by vector  Vertical concatenation is simple: just concatenate the two vectors  ‘Pictures’ will line up along their left margin  No predefined concatenation of vectors, let’s define one: vector vcat( vector const& top, vector const& bottom); 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 18

Vertical Concatenation vector vcat( vector const& top, vector const& bottom) { // copy the top picture vector ret = top; // copy entire bottom picture for (auto const& s: bottom) { ret.push_back(s); } return ret; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 19

Vertical Concatenation vector vcat( vector const& top, vector const& bottom) { // copy the top picture vector ret = top; // copy entire bottom picture, use iterators for (auto it = bottom.begin(); it != bottom.end(); ++it) { ret.push_back(*it); } return ret; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 20

Vertical Concatenation vector vcat( vector const& top, vector const& bottom) { // copy the top picture vector ret = top; // copy entire bottom picture, use vector facilities ret.insert(ret.end(), bottom.begin(), bottom.end()); return ret; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 21

Horizontal Concatenation For example: this is an ************** example * this is an * to * example * illustrate * to * framing * illustrate * * framing * ************** 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 22

Horizontal Concatenation If left hand side picture is shorter than right hand side, we need padding Otherwise just copy the picture Interface similar to vcat: vector hcat( vector const& left, vector const& right) { // get width of left, add 1 to leave a space between pictures // handle all rows from both pictures, line by line // copy a row from the left-hand side, if there is one // pad the line to full width of left + 1 // copy row from the right-hand side, if there is one // append overall line to result vector } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 23

Horizontal Concatenation vector hcat(vector const& left, vector const& right) { vector ret; // add 1 to leave a space between pictures auto width1 = width(left) + 1; auto i = 0, j = 0; // continue until we've seen all rows from both pictures while (i != left.size() || j != right.size()) { string s; // construct new string to hold characters from both pictures // copy a row from the left-hand side, if there is one if (i != left.size()) s = left[i++]; s += string(width1 - s.size(), ' '); // pad to full width // copy a row from the right-hand side, if there is one if (j != right.size()) s += right[j++]; ret.push_back(s); // add s to the picture } return ret; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 24

Vertically Flip a Picture For example: this is an flipping example illustrate to illustrate example flipping this is an 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 25

Vertically Flip a Picture Straight forward solution: vector vflip(vector const& v) { vector ret; for (auto it = v.rbegin(); it != v.rend(); ++it) ret.push_back(*it); return ret; } Using Standard algorithm vector vflip(vector const& v) { vector ret; reverse_copy(ret.begin(), ret.end(), back_inserter(ret)); return ret; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 26

Rotate a Picture For example: this is an n e example a t to an illustrate se ro rotation il ti p st sm ua ia lt hxolo tetir 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 27

Rotate a Picture vector rotate_left(vector const& v) { vector ret; // take a letter from each string, starting at the end auto maxlen = width(v); for (auto i = maxlen; i != 0; --i) { string line; // new line // for all lines in the input image for (auto const& current: v) { if (current.size() < i) line += ' '; else line += current[i-1]; } // store the new line in the result picture ret.push_back(line); } return ret; } 3/3/2015, Lecture 11 CSC 1254, Spring 2015, Taking Strings Apart 28