Strings theory + first problem: build a health check URL
Problem statement
A string is text, any sequence of characters wrapped in quotes: "api.internal", "8443", "/healthz". You've already printed strings and combined a couple of them together. This problem is the same idea with more pieces: building one longer string out of several smaller values.
Common mistake: in C++, trying to add a number directly onto a string with +.
Wrong: std::string url = "host:" + port;C++'s + only works between two std::strings (or a string and a single character), not between a string and an int. port needs to be converted first with std::to_string(port). Python and Java don't have this problem the same way: Python needs an f-string or str(port), and Java's + silently converts numbers to text for you.
Your task: given host = "api.internal", port = 8443, and path = "/healthz", build the full URL and print it.
Expected output:
https://api.internal:8443/healthzApproach
Each language builds the combined string a little differently:
- Python uses an f-string,
f"https://{host}:{port}{path}", which drops each variable straight into the text, converting numbers automatically. - Java uses
+to join everything,"https://" + host + ":" + port + path, and convertsportto text automatically because it's anintnext to aString. - C++ needs
portconverted explicitly first, withstd::to_string(port), since+between astd::stringand anintwon't compile.
host = "api.internal"
port = 8443
path = "/healthz"
url = f"https://{host}:{port}{path}"
print(url)