Frage im Vorstellungsgespräch bei Microsoft

Write the pseudocode to reverse a string

Antworten zu Vorstellungsgespräch

Anonym

28. Nov. 2012

In response to Harry's comment, I have an answer for that :) String s = "hello my name is zach galefernachus"; String temp = s.substring(s.lastIndexOf(" ")+1); s = " " +s+ " "; for(int i = s.length()-2; i>=0; i--){ if(s.substring(i, i+1).equals(" ")){ temp += s.substring(i, s.indexOf(" ", i+1)); } } return temp;

1

Anonym

10. März 2013

To post 1: More efficient it will be to change 2 elements. It halves the time: for(int i = 0; i > s.length/2; i--){ char temp; temp = string[i]; string[i] = srting[string.length - i]; srting[string.length - i] = temp; } of course string.length should be calculated once only and saved to some int. I think that works faster.

Anonym

10. März 2013

By the way, do you need to create a new string?

Anonym

9. Nov. 2011

reverse(string s){ String returnString = ""; for(int i = s.length -1; i >= 0; i--){ returnString+=s[i]; } return returnString }

3

Anonym

14. Nov. 2011

It is easy to just reverse a string. It is more tricky to reverse the order of all words in a sentences. For instance, if the input is "We are happy", the output is "happy are We". The detailed analysis is on a blog: http://codercareer.blogspot.com/2011/09/no-07-reverse-words-in-sentence.html