wasAsked.

Write code to reverse the words in "I am developer".

Altimetrik · Role not specified · technical

Published

Asked in 2026 · Software Engineering

Community answers

Posted by wasAsked editorial collection

Goal: "I am developer" becomes "developer am I". Simple solution with built-in methods string input = "I am developer"; string result = string.Join(" ", Enumerable.Reverse(input.Split(' ', StringSplitOptions.RemoveEmptyEntries))); Console.WriteLine(result); // developer am I StringSplitOptions.RemoveEmptyEntries handles multiple spaces between words. Calling Enumerable.Reverse explicitly matters: with C# 14's span conversions, writing array.Reverse() can bind to the in-place MemoryExtensions.Reverse for spans, which returns void and breaks the expression. string[] words = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); Array.Reverse(words); string result2 = string.Join(" ", words); Manual solution without built-in reverse (often what interviewers want to see) static string ReverseWords(string sentence) { var words = new List<string>(); var current = new StringBuilder(); foreach (char ch in sentence) { if (ch == ' ') { if (current.Length > 0) { words.Add(current.ToString()); current.Clear(); } } else current.Append(ch); } if (current.Length > 0) words.Add(current.ToString()); var result = new StringBuilder(); for (int i = words.Count - 1; i >= 0; i--) { result.Append(words[i]); if (i > 0) result.Append(' '); } return result.ToString(); } Console.WriteLine(ReverseWords("I am developer")); // developer am I Variation: reverse the characters of each word but keep the word order ("I ma repoleved") string eachWordReversed = string.Join(" ", input.Split(' ').Select(w => new string(w.Reverse().ToArray()))); Discussion points - Complexity: O(n) time and O(n) extra space. - Edge cases: empty string, a single word, leading or trailing spaces, multiple spaces, and punctuation attached to words ("developer." stays with the word). - Clarify whether to preserve original spacing; the solutions above normalise to single spaces. - An in-place approach on a char array is possible: reverse the whole array, then reverse each word — useful when asked to minimise extra memory.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics