Lesson 2

String Parsing and Arithmetic with C++

Introduction

Welcome! Today, we have an intriguing and practical task at hand that will test your C++ programming skills. We will be maneuvering around parsing strings and making type conversions. So, let's launch into it!

Task Statement and Description

Our task for the day involves creating a C++ function called ParseAndMultiplyNumbers(). This function is designed to accept a string as an input. However, it's not just any string — the input we're considering is a playful mix of numbers and words.

The purpose of this function is to analyze the input string, extract all the numbers, convert these numbers (currently string types) into integer data types, and then multiply all these numbers together. The final output? It's the product of all those numbers!

Here's an illustration for clarification. Given the input string "I have 2 apples and 5 oranges," our function should return the product of 2 and 5, which is 10.

Step-by-Step Solution Building: Step 1

The primary task is to parse the string and identify the numbers. To do that, let's create an empty string num to accumulate digits and a vector numbers to collect all the numbers we find:

C++
1std::string inputString = "I have 2 apples and 5 oranges"; 2std::string num; 3std::vector<int> numbers;
Step-by-Step Solution Building: Step 2

The next step requires iterating through the input string character by character. When we encounter a digit, we append it to our num string. If a character isn’t a digit and num isn’t empty, it means we've reached the end of a number.

At this point, we convert num to an integer, add it to the numbers vector, and reset num to an empty string. If the character isn’t a digit and num is empty, we simply skip and progress.

C++
1for (char ch: inputString) { 2 if (isdigit(ch)) { 3 num += ch; 4 } else if (!num.empty()) { 5 numbers.push_back(stoi(num)); 6 num = ""; 7 } 8} 9for (int number: numbers) { 10 std::cout << number << " "; 11}

After running this code, the output should be "2 5".

Step-by-Step Solution Building: Step 3

Finally, we multiply all the numbers in the vector numbers together. The multiplication result gets stored in the result variable.

C++
1int result = 1; 2for (int number: numbers) { 3 result *= number; 4} 5std::cout << result;

After executing this code, the console output should be 10.

Full Solution

Bringing together all the steps, our final C++ solution manifests as follows:

C++
1#include <vector> 2#include <iostream> 3 4int ParseAndMultiplyNumbers (std::string inputString) { 5 std::string num; 6 std::vector<int> numbers; 7 for (char ch: inputString) { 8 if(isdigit(ch)) { 9 num += ch; 10 } else if (!num.empty()) { 11 numbers.push_back(stoi(num)); 12 num = ""; 13 } 14 } 15 if (!num.empty()) { 16 numbers.push_back(stoi(num)); 17 } 18 19 int result = 1; 20 for(int number: numbers) { 21 result *= number; 22 } 23 return result; 24} 25 26int main() { 27 // Call the function 28 std::cout << ParseAndMultiplyNumbers("I have 2 apples and 5 oranges") << std::endl; 29 return 0; 30}

This solution also caters to numbers situated at the end of the input string.

Lesson Summary

Applaud yourself! You've successfully developed a C++ function that deftly navigates through strings to identify numbers, performs a data type conversion, and then conducts an arithmetic operation on those numbers. You've truly demonstrated admirable skill in orchestrating these coding concepts!

However, as always in coding, practice is the key to improvement. With this solution, you could try to perform different operations on the numbers or change the condition for identifying valid numbers, thereby further sharpening your C++ skills. Every challenge helps improve your core C++ skills. Here's to coding greatness!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.