Interview Question

Junior Software Engineer Interview

-Richmond, BC


Write a function that reverses the order of words in a sentence.


Interview Answers

2 Answers


Assuming they're using C# string sentence = Console.ReadLine(); // Assign input to string string[] sentenceWords = sentence.Split(' '); // Assign words sequentially into an array Array.Reverse(sentenceWords); // Reverse array Console.WriteLine(String.Join(" ", sentenceWords)); // Show result

Matt Carter on


C implementation... Copy the sentence into a character array. Reverse all the characters in the array, then reverse the characters for each word. Word separator is space.

Theo on

Add Answers or Comments

To comment on this, Sign In or Sign Up.