Skip to main content
Programming from zero

Normalize a config value

easyStrings

Problem statement

Config values read from a file or environment variable often come with stray whitespace or inconsistent capitalization: " ENABLED ", "Enabled", "enabled\n" might all be meant to represent the same setting. Comparing them directly with == would treat them as different values, so you normalize the string first: strip the extra whitespace, then lowercase it, so every version becomes the same thing.

flowchart LR A["' ENABLED '"] -->|"strip spaces"| B["'ENABLED'"] B -->|"make lowercase"| C["'enabled'"] classDef before fill:#FEE2E2,stroke:#DC2626,color:#991B1B classDef middle fill:#FEF3C7,stroke:#D97706,color:#92400E classDef after fill:#D1FAE5,stroke:#059669,color:#065F46 class A before class B middle class C after

Common mistake: normalizing only one of the two steps.

TEXT
Wrong: value.lower() # spaces still there: " enabled "

Lowercasing alone doesn't remove the surrounding whitespace, " enabled " still won't equal "enabled". Both steps, trim and lowercase, are needed together.

Your task: given raw = " ENABLED ", normalize it and print the result.

Expected output:

TEXT
enabled

Approach

Every language has a trim method and a lowercase method; the difference is only in what they're called:

  • Python: .strip() removes leading/trailing whitespace, .lower() lowercases.
  • Java: .trim() and .toLowerCase(), called the same way, chained together.
  • C++ doesn't have a built-in trim, so it's done manually with <algorithm>, finding the first and last non-space character and keeping only what's between them. Lowercasing uses std::transform with ::tolower applied to every character.
raw = "  ENABLED  "
normalized = raw.strip().lower()
print(normalized)