有关尉迟方兄遇到的面试题。
1、reverse a sentence in place.
例子:I am yuchifang -> yuchifang am I
注意in place
例子:I am yuchifang -> yuchifang am I
注意in place
嘿嘿!这个题做出来了,迟方兄,我这次是注意了in place了的,在有限的空间里进行转换,所以我只用了一个temp来做临时存放用,而这个存放的也不是整个句子的数组,只是一个单词,这样,先把句子全倒转过来,然后再一个单词一个单词的倒转,那么在转换时就只多用出一个单词的位置来进行处理。
程序函数如下:
Public Function result(ByVal souce As String) As String
souce = Trim(souce)
Dim i, j As Integer
Dim temp As String = Nothing
j = souce.Length
souce = StrReverse(souce)
For i = 1 To j
If Mid(souce, i, 1) <> " " Then temp = Mid(souce, i, 1) & temp
If Mid(souce, i, 1) = " " Or i = j Then
souce = Replace(souce, StrReverse(temp), temp, 1, 1, CompareMethod.Text)
temp = Nothing
End If
Next
Return souce
End Function
这次我是仔细测试过了的。
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这次我是仔细测试过了的。
replace的参数里,那两个1,表示从第一个字符起搜索,只替换一个。
StrReverse()这个函数是vb.net里的将字符串的字符倒过来排列的函数
StrReverse()这个函数是vb.net里的将字符串的字符倒过来排列的函数