交换字符串中的单词 [英] Swap words in a string
问题描述
我想编写代码以交换字符串中的单词.
输入-我喜欢蛋糕
输出-我喜欢的蛋糕
为此,如果有一种方法可以确定每个单词的索引,那就太好了.就像
1-我
2-喜欢
3-蛋糕
然后我可以交换单词.但是我不知道该怎么做.
目前,我已编写代码以计算字符串中的单词数并获得平均值.
i want to write a code to swap the words in a string.
Input - I like cake
Output - cake I like
To do this it would be great if there is a way to determine the index of each word.Like
1- I
2- like
3- cake
Then I can swap the words.But I have no idea how to do it.
Currently I have written the code to count the number of words in a string and to get the average.
Dim str As String = Me.t1.Text
Dim strA() As String = str.Split(" ")
Dim TChrs As Int32
Dim i As Int32
For i = strA.Length - 1 To 0 Step -1
TChrs += strA(i).Length
Next
MsgBox("Number of words = " & strA.Length)
MsgBox("Average Length = " & TChrs / strA.Length)
谁能帮我这个忙
Can any one please help me with this
推荐答案
使用以下代码:
Use the following code :
Dim strSearchWords As String = "I like apple"
Dim sarySearchWord As String()
sarySearchWord = Split(Trim(strSearchWords), " ")
MsgBox("Number of words = " & sarySearchWord.Length
您可以通过掉期搜索这些词:
You can Search these words by Swaping :
Dim s1 As String = sarySearchWord(0) & sarySearchWord(1) & sarySearchWord(3)
Dim s2 As String = sarySearchWord(1) & sarySearchWord(0) & sarySearchWord(3)
Dim s3 As String = sarySearchWord(1) & sarySearchWord(3) & sarySearchWord(2
输出:
Output :
MsgBox(s1) : I like apple
MesgBox(s2) : like I apple
MsgBox(s3) : like apple I
希望对您有所帮助:)
I hope it will help you :)
这是我使用Heap算法确定输入字符串所有可能排列的新解决方案.请给我评论,让我知道它如何为您服务.将其标记为答案也将很好. :)
从C#更改为VB(我忘了):)
Here is my new solution for you using Heap''s algorithm to determine all possible permutations of your input string. Please leave me a comment and let me know how it worked for you. Marking this as an answer would be nice too. :)
changed from C# to VB (I forgot) :)
Private Sub Form1_Load(sender As Object, e As EventArgs)
Dim word As String = "I like cake"
Dim split As String() = word.Split(New Char() {" "c},
StringSplitOptions.RemoveEmptyEntries)
Dim words As New List(Of String)()
HeapPermute(words, split, split.Length)
' "words" now contains all possible permutations of "word"
End Sub
Private Sub HeapPermute(words As List(Of String), split As String(), splitCount As Integer)
Dim n As Integer = splitCount
If n = 1 Then
'add item
Dim temp As String = ""
For i As Integer = 0 To split.Length - 1
temp += split(i) & " "
Next
words.Add(temp.TrimEnd(" "c))
Else
For i As Integer = 0 To n - 1
HeapPermute(words, split, (n - 1))
If n Mod 2 = 1 Then
'odd
Dim temp As String = split(0)
split(0) = split(n - 1)
split(n - 1) = temp
Else
'even
Dim temp As String = split(i)
split(i) = split(n - 1)
split(n - 1) = temp
End If
Next
End If
End Sub
这是我测试此特定字符串的输出.
我喜欢蛋糕
像我蛋糕一样
我喜欢的蛋糕
我喜欢
像蛋糕我
像我这样的蛋糕
/////////////////////////////////////////////////////////////
原始解决方案
////////////////////////////////////////////////////////////
试试这个
And here is the output from my test on this particular string.
I like cake
like I cake
cake I like
I cake like
like cake I
cake like I
//////////////////////////////////////////////////////////
Original Solution
/////////////////////////////////////////////////////////
Try this
Dim original As String = "I like cake"
Dim words As String() = original.Split(New Char() {" "},
StringSplitOptions.RemoveEmptyEntries)
Array.Reverse(words)
Dim reversed As String = String.Join(" ", words)
反向将是像我一样的蛋糕"
/////////////////////////////////////////////////////////////
END原始解决方案
////////////////////////////////////////////////////////////
reversed will be "cake like I"
//////////////////////////////////////////////////////////
END Original Solution
/////////////////////////////////////////////////////////
我不知道VB,但是假设str.Split(" ")
命令返回一个由字符串组成的单词数组,您只需要对该数组进行索引从最后到第一.
I don''t know VB, but assuming the str.Split(" ")
command returns an array of the words of the string, you just need to index that array from last to first.
这篇关于交换字符串中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!