REVIEW - Functions and Streams In this worksheet, you will practice writing functions involving problems that use streams (stringstreams, file streams) and String methods (find, substr) Exercise 1 Following is a code snippet for a program that reads several lines from a file¹. It prints words ignoring commas and whitespace in each line. Lastly, it prints the number of such words. //Extract each line while (getline(namelFS, line)) { } int count = 0; //From the line, extract words up to comma istringstream lineSS(line); string words; while (getline(lineSS, words, ',')) { //Extract each word from the words string word2; istringstream wordSS(words); while (wordSS >> word2) { } cout << word2 << " "; ++count; } cout << count << endl; Example file data.txt: why did the chicken cross the playground? Because, it wanted to go to the other slide! Ha, ha ha ha " Output file data_parse.txt: why did the chicken cross the playground? 7 Because it wanted to go to the other slide! 9 Ha ha ha ha 4 Let us divide the above code into two functions and rewrite it. Such a process is called refactoring (which you also did for the last problem in Lab 03). The two functions you need to write are in the following two pages.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
REVIEW - Functions and Streams
In this worksheet, you will practice writing functions involving problems that use streams
(stringstreams, file streams) and String methods (find, substr)
Exercise 1
Following is a code snippet for a program that reads several lines from a file¹. It prints words
ignoring commas and whitespace in each line. Lastly, it prints the number of such words.
//Extract each line
while (getline(namelFS, line))
{
int count = 0;
//From the line, extract words up to comma
istringstream lineSS(line);
string words;
while (getline(lineSS, words, ','))
{
//Extract each word from the words
string word2;
istringstream wordSS(words);
while (wordSS >> word2)
{
}
cout << word2 << " ";
++count ;
}
cout << count << endl;
Example file data.txt:
why did the chicken cross the playground?
Because, it wanted to go to the other slide!
Ha, ha, ha ha
Output file data_parse.txt:
why did the chicken cross the playground? 7
Because it wanted to go to the other slide! 9
Ha ha ha ha 4
Let us divide the above code into two functions and rewrite it. Such a process is called
refactoring (which you also did for the last problem in Lab 03). The two functions you need to
write are in the following two pages.
Transcribed Image Text:REVIEW - Functions and Streams In this worksheet, you will practice writing functions involving problems that use streams (stringstreams, file streams) and String methods (find, substr) Exercise 1 Following is a code snippet for a program that reads several lines from a file¹. It prints words ignoring commas and whitespace in each line. Lastly, it prints the number of such words. //Extract each line while (getline(namelFS, line)) { int count = 0; //From the line, extract words up to comma istringstream lineSS(line); string words; while (getline(lineSS, words, ',')) { //Extract each word from the words string word2; istringstream wordSS(words); while (wordSS >> word2) { } cout << word2 << " "; ++count ; } cout << count << endl; Example file data.txt: why did the chicken cross the playground? Because, it wanted to go to the other slide! Ha, ha, ha ha Output file data_parse.txt: why did the chicken cross the playground? 7 Because it wanted to go to the other slide! 9 Ha ha ha ha 4 Let us divide the above code into two functions and rewrite it. Such a process is called refactoring (which you also did for the last problem in Lab 03). The two functions you need to write are in the following two pages.
Expert Solution
steps

Step by step

Solved in 5 steps with 1 images

Blurred answer
Knowledge Booster
Stack operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education