From ab4c4f5aed3c4795afdbbb6f113ce57487aefb93 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 15 Feb 2024 15:53:44 -0500 Subject: [PATCH] adding provided code --- hws/05_online_dating/README.md | 36 +++++++++++++++++++++ hws/05_online_dating/provided_code/test.cpp | 16 +++++++++ 2 files changed, 52 insertions(+) create mode 100644 hws/05_online_dating/provided_code/test.cpp diff --git a/hws/05_online_dating/README.md b/hws/05_online_dating/README.md index 24946a1..53fc0e5 100644 --- a/hws/05_online_dating/README.md +++ b/hws/05_online_dating/README.md @@ -211,6 +211,42 @@ This function takes four parameters, which are the latitude and longitude of two #include ``` +## Other Useful Code + +Given a long string like this: "663-979-6253_953-451-3708_410-750-5502_750-260-3152_688-574-6330_915-954-4073", how do I get each phone number? You can use code like this: + +```cpp +#include +#include // include this so that we can use stringstream + +int main(){ + + std::string longString = "663-979-6253_953-451-3708_410-750-5502_750-260-3152_688-574-6330_915-954-4073"; + // create a stringstream to tokenize the long string + std::istringstream iss(longString); + std::string token; + + // tokenize the long string using the underscore delimiter + while (std::getline(iss, token, '_')) { + std::cout << token << std::endl; + } + return 0; +} +``` + +This program will print the following to the console: + +```console +663-979-6253 +953-451-3708 +410-750-5502 +750-260-3152 +688-574-6330 +915-954-4073 +``` + +You can try to compile and run this tiny [program](provided_code/test.cpp) yourself. + ## Program Requirements & Submission Details This assignment has some unique requirements: In this assignment, you are required to create and manipulate linked lists using nodes, **you are not allowed to use the std::list library. You are not allowed to define a List class or an iterator class. You are not allowed to use any data structures we have not learned so far.** diff --git a/hws/05_online_dating/provided_code/test.cpp b/hws/05_online_dating/provided_code/test.cpp new file mode 100644 index 0000000..8831041 --- /dev/null +++ b/hws/05_online_dating/provided_code/test.cpp @@ -0,0 +1,16 @@ +#include +#include // include this so that we can use stringstream + +int main(){ + + std::string longString = "663-979-6253_953-451-3708_410-750-5502_750-260-3152_688-574-6330_915-954-4073"; + // create a stringstream to tokenize the long string + std::istringstream iss(longString); + std::string token; + + // tokenize the long string using the underscore delimiter + while (std::getline(iss, token, '_')) { + std::cout << token << std::endl; + } + return 0; +}