Reverse each word of a sentence with starting letter in CAPS
Filed under SAP
Free read 3 of 3Split the input sentence into individual words using the space character as a delimiter.
The question
Write a program to reverse each word in a given sentence. After reversing, ensure that the first letter of every reversed word is capitalized, and all other letters in that word are lowercase.
Constraints
- Sentence length <= 1000
- Words are separated by single spaces
- Input contains only alphabetic characters and spaces
Examples
- Input
- "Hello World"
- Output
- "Olleh Dlrow"
- Why
- "Hello" reversed is "olleH", capitalized is "Olleh". "World" reversed is "dlroW", capitalized is "Dlrow".
Approach
Split the input sentence into individual words using the space character as a delimiter.
For each word, reverse the sequence of characters (e.g., 'abc' becomes 'cba').
Apply a capitalization transformation to each reversed word: first character uppercase, the rest lowercase.
Join the transformed words back into a single string separated by spaces.
Solution
O(N) time, O(N) space
def reverse_and_capitalize(sentence):
words = sentence.split(' ')
result = []
for word in words:
if not word: continue
# Reverse word
reversed_word = word[::-1]
# Capitalize first, lowercase rest
result.append(reversed_word.capitalize())
return ' '.join(result)
Tips
- What they are testing: Test with single-letter words and sentences with varying casing (e.g., all caps or all lowercase) to ensure normalization works.
- Common mistake: Reversing the entire sentence instead of individual words, or forgetting to lowercase the characters that were originally uppercase but are no longer at the start.
Related SAP questions
Collected from public candidate posts and coding forums. Updated Nov 2026.