MDA Corporation Interview Question: Write a function that reverse... | Glassdoor

Interview Question

Junior Software Engineer Interview Richmond, BC (Canada)

Write a function that reverses the order of words in a

  sentence.
Answer

Interview Answer

2 Answers

0

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 Jun 20, 2013
0

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 Nov 14, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.