Skip to main content
Programming from zero

Strings theory + first problem: build a health check URL

easyStrings Must-do

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.

flowchart LR A["host = 'api.internal'"] --> D["Combine into one URL"] B["port = 8443"] --> D C["path = '/healthz'"] --> D D --> E["https://api.internal:8443/healthz"] classDef part fill:#DBEAFE,stroke:#2563EB,color:#1E3A8A classDef combine fill:#E0E7FF,stroke:#4F46E5,color:#312E81 classDef output fill:#D1FAE5,stroke:#059669,color:#065F46 class A part class B part class C part class D combine class E output

Common mistake: in C++, trying to add a number directly onto a string with +.

TEXT
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:

TEXT
https://api.internal:8443/healthz

Approach

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 converts port to text automatically because it's an int next to a String.
  • C++ needs port converted explicitly first, with std::to_string(port), since + between a std::string and an int won't compile.
host = "api.internal"
port = 8443
path = "/healthz"
url = f"https://{host}:{port}{path}"
print(url)