First Letter Capitalization II - Problem

You have a table user_content containing text content that needs to be properly formatted.

Table: user_content

Column NameType
content_idint
content_textvarchar

content_id is the unique key for this table. Each row contains a unique ID and the corresponding text content.

Write a solution to transform the text in the content_text column by applying the following rules:

  • Convert the first letter of each word to uppercase and the remaining letters to lowercase
  • Special handling for words containing special characters:
    • For words connected with a hyphen -, both parts should be capitalized (e.g., top-ratedTop-Rated)
  • All other formatting and spacing should remain unchanged

Return the result table that includes both the original content_text and the modified text following the above rules.

Table Schema

user_content
Column Name Type Description
content_id PK int Unique identifier for each content row
content_text varchar Text content to be formatted
Primary Key: content_id
Note: Each row contains unique text content that may include hyphenated words

Input & Output

Example 1 — Basic Text with Hyphenated Words
Input Table:
content_id content_text
1 hello world-class service
2 top-rated product review
3 user-friendly interface design
Output:
content_id original_text formatted_text
1 hello world-class service Hello World-Class Service
2 top-rated product review Top-Rated Product Review
3 user-friendly interface design User-Friendly Interface Design
💡 Note:

Each word's first letter is capitalized, and for hyphenated words like 'world-class', both parts are capitalized as 'World-Class'.

Example 2 — Mixed Case and Special Formatting
Input Table:
content_id content_text
4 API documentation
5 self-service portal
6 twenty-four hour support
Output:
content_id original_text formatted_text
4 API documentation Api Documentation
5 self-service portal Self-Service Portal
6 twenty-four hour support Twenty-Four Hour Support
💡 Note:

All text is converted to proper case with first letters capitalized. Note that 'API' becomes 'Api' as the rule converts to lowercase first, then capitalizes first letters.

Constraints

  • 1 ≤ content_id ≤ 1000
  • content_text contains only letters, spaces, and hyphens
  • 1 ≤ length(content_text) ≤ 500

Visualization

Tap to expand
First Letter Capitalization II INPUT user_content id content_text 1 hello-world test 2 self-driving car 3 well-known phrase Raw text with: - lowercase words - hyphenated words - multiple spaces ALGORITHM STEPS 1 Split by Spaces Tokenize text into words 2 Check for Hyphens Identify hyphenated words 3 Capitalize Parts Cap each hyphen segment 4 Rejoin Text Combine all processed words Example Transform: "hello-world" --> "Hello-World" Both parts capitalized! FINAL RESULT formatted_content id original modified 1 hello-world test Hello-World Test 2 self-driving car Self-Driving Car 3 well-known phrase Well-Known Phrase OK - All Formatted! Both columns returned SQL Approach: INITCAP() or REGEXP_REPLACE() with custom logic Key Insight: Hyphenated words require special handling - each part after a hyphen must also be capitalized. Use REPLACE + UPPER combination or REGEXP_REPLACE with capture groups to transform "hello-world" into "Hello-World" while preserving the hyphen structure. TutorialsPoint - First Letter Capitalization II | Optimal Solution
Asked in
Meta 28 Amazon 22 Google 18
25.4K Views
Medium Frequency
~18 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen