Motivation Parsing functions Homework 8. Text Parsing Ján Dugáček November 14, 2018 Ján Dugáček 8. Text Parsing Motivation Parsing functions Homework Table of Contents 1 Motivation 2 Parsing functions std::getline std::stringstream std::string Exercise 3 Homework Ján Dugáček 8. Text Parsing Motivation Parsing functions Homework Motivation What if the file you need to read can’t be read by easy::vector? Ján Dugáček 8. Text Parsing Motivation Parsing functions Homework std::getline std::stringstream std::string Exercise std::getline easy : : vector l i n e s ( 1 ) ; wh il e ( std : : g e t l i n e ( in , l i n e s . back ( ) , ’ \n ’ )) { i f ( ! l i n e s . back ( ) . empty ( ) ) l i n e s . push_back ( ) ; } std::getline reads from the input in its first argument (it can be std::cin, std::ifstream, ...), saves it into the string on its second argument until it finds the separator in its last parameter (new line by default) This program splits the input into lines, ignoring the empty ones Can you spot the error in the code? Ján Dugáček 8. Text Parsing Motivation Parsing functions Homework std::getline std::stringstream std::string Exercise Function #2 #i n c l u d e // . . . easy : : vector b l o c k s ; std : : s t r i n g s t r e a m stream ( l i n e s [ i ] ) ; std : : s t r i n g reading ; wh il e ( std : : g e t l i n e ( stream , reading , ’ \ t ’ )) { b l o c k s . push_back ( ) ; std : : swap ( b l o c k s . back ( ) , reading ) ; } stringstream can be used to parse a string in the same way as std::cin or std::ifstream Ján Dugáček 8. Text Parsing Motivation Parsing functions Homework std::getline std::stringstream std::string Exercise std::string std : : s t r i n g t e x t = "ahoy" ; t e x t . push_back ( ’ ! ’ ) ; char f i r s t L e t t e r = t e x t [ 0 ] ; i f ( f i r s t L e t t e r >= ’A ’ && f i r s t L e t t e r <= ’Z ’ ) f i r s t L e t t e r += ’A ’ − ’ a ’ ; t e x t [ 0 ] = f i r s t L e t t e r ; i f ( t e x t == "Ahoy ! " ) t e x t . append ( " Pleased to see you ! " ) ; std::string can do plenty of stuff, but it may be impractical Mistakes or unhandled invalid inputs can cause crashes Ján Dugáček 8. Text Parsing Motivation Parsing functions Homework std::getline std::stringstream std::string Exercise std::string #2 i n t number = 4; std : : s t r i n g t e x t = " I t ’ s " + std : : to_string ( number ) ; t e x t += " ! " ; i n t l ength = t e x t . s i z e ( ) ; f o r ( unsigned i n t i = 0; t e x t [ i ] != 0; i++) { i f ( t e x t [ i ] >= ’ 0 ’ && t e x t [ i ] <= ’ 9 ’ ) length −−; } i n t number2 = std : : s t o i ( std : : to_string ( number ) ) ; std : : s t r i n g theNumber = t e x t . s u b s t r (5 , 2 ) ; std::to_string can be used to convert a number to string std::stoi can be used to convert a string to an integer (std::stof for float) The final symbol in a string is always 0 The substr method (in this case) returns a string containing 2 characters starting with the fifth Ján Dugáček 8. Text Parsing Motivation Parsing functions Homework std::getline std::stringstream std::string Exercise Exercise 1 Write a function that finds the number in string like The number is 14 probably. 2 Write a function that splits a comma-separated list string (like toaster,kettle,dishwasher) 3 Write a function that checks if a string is a number 4 Write a function that splits a string into capital letters, small letters and numbers and dumps the rest 5 Write a function that multiplies all numbers placed inside a string by a number given in the second argument Ján Dugáček 8. Text Parsing Motivation Parsing functions Homework Homework Write a function that parses files as shown into a vector of vectors that are entries containing the x, y and z parametres respectively x=1 y=2 z=3 y=1.1 x=8.2 z=0.3 z=0.1 y=12 x=6.1 You have two weeks to do it Ján Dugáček 8. Text Parsing