public string Reverse(StringBuilder input, int start, int end) { _inputStringValidator.Validate(input.ToString()); while (start < end) { char temp = input[start]; input[start] = input[end]; input[end] = temp; start++; end--; } return(input.ToString()); }
public string Reverse(string inputString, IInputStringValidator inputStringValidator) { var outputString = ""; _inputStringValidator.Validate(inputString); Stack <string> wordStack = new Stack <string>(inputString.Split(' ').Select(o => o.Trim())); while (wordStack.Count > 0) { outputString += wordStack.Pop() + ((wordStack.Count >= 1) ? " " : ""); } return(outputString); }
public string Reverse(string inputString, IInputStringValidator inputStringValidator) { _inputStringValidator.Validate(inputString); StringBuilder outputString = new StringBuilder(_reverserStrategy.Reverse(new StringBuilder(inputString), 0, inputString.Length - 1)); int start = 0; int end = inputString.Length - 1; bool flag = false;//to identify if we are already traversing a word. for (int i = 0; i < outputString.Length; i++) { if (!flag) { if (outputString[i] != ' ') { start = i; end = i; flag = true; } } else { if (i + 1 < outputString.Length && outputString[i + 1] == ' ')//end of word { end = i; _reverserStrategy.Reverse(outputString, start, end); flag = false; } else if (i + 1 == outputString.Length)//end of string { end = i; _reverserStrategy.Reverse(outputString, start, end); flag = false; } } } return(outputString.ToString()); }