Programming Write a function that reads a line and reverses

Programming: Write a function that reads a line and reverses the words in the line (not the characters) using a stack.

For example, given the following input: The quick brown fox jumps over the lazy dog.

You should get the following output: dog. lazy the over jumps fox brown quick The

Solution

public static string reverseWords(string sentence)

{

string[] words = sentence.split(\'//s+\');

stack<string> stack = new stack<string>();

for(string word : words)

{

stack.push(word);

}

stringBuilder stb=new stringBuilder();

while(!stack.empty())

{

stb.append(stack.pop());

if(!stack.empty())

{

stb.append(\" \");

}

}

return stb.tostring();

}

Programming: Write a function that reads a line and reverses the words in the line (not the characters) using a stack. For example, given the following input: T

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site