AlgoInsight

DSA problem

Convert a String to Title Case

EasyStringsPattern: String TraversalbeginnerFree

Given a string s, convert it to title case - capitalize the first letter of each word and make the rest lowercase.

Walk the string and capitalize the first character of every word (the first character overall and any character that follows a space), lowercasing the rest. Edge cases to handle: multiple consecutive spaces, leading or trailing spaces, empty words, and single-character words. This is a practical string-manipulation task seen in data cleaning, UI formatting, and content management systems. Python has str.title(), but it mishandles cases like it's (which becomes It'S); a manual implementation gives full control over the capitalization logic.

Examples

Example 1
Input: s = "hello world from algoinsight"
Output: "Hello World From Algoinsight"
Explanation: The first letter of each word is capitalized and the remaining letters are lowercased.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

string

Browse all DSA problems