Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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.

VB
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
Posted
Updated 29-Nov-11 2:42am
v2
Comments
Manoj K Bhoir 29-Nov-11 8:42am    
Edited for formatting!
elgaabeb 29-Nov-11 8:45am    
Which programming language are you using ??
Janaka Medawala 29-Nov-11 10:19am    
VB.net
RaviRanjanKr 29-Nov-11 9:11am    
You should Mention programming Langue in your Question to get appropriate answer.
Janaka Medawala 29-Nov-11 10:20am    
Sorry Mr.Ravi.Im using vb.net

Use the following code :
VB
        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 :
VB
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 :
VB
MsgBox(s1) : I like apple
MesgBox(s2) : like I apple
MsgBox(s3) : like apple I

I hope it will help you :)
 
Share this answer
 
Comments
Janaka Medawala 29-Nov-11 12:30pm    
Thank you so much for your code Mr.Manoj

Can you suggest me a code or an alogorithm to implement this code on any number of words.Thats what Im looking for.
LanFanNinja 29-Nov-11 22:39pm    
Recheck my solution(solution 4).
LanFanNinja 29-Nov-11 14:45pm    
My +5
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. :)

EDIT: changed from C# to VB (I forgot) :)

VB
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
VB
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)


reversed will be "cake like I"

//////////////////////////////////////////////////////////
END Original Solution
/////////////////////////////////////////////////////////
 
Share this answer
 
v8
Comments
RaisKazi 29-Nov-11 9:42am    
Edited: Added "pre" tag.
LanFanNinja 29-Nov-11 9:44am    
Thank you! I totally forgot! :)
RaisKazi 29-Nov-11 10:08am    
Welcome. :)
Janaka Medawala 29-Nov-11 12:35pm    
Thanks Ninja.
But I want a code that gives an output like this
I cake like
cake I like
cake like I
like cake I
like I cake
LanFanNinja 29-Nov-11 19:11pm    
I have updated my solution with the algorithm I kinda sorta promised. Please check it out I think it will fit your needs nicely.
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.
 
Share this answer
 
You can split a string into an array of substrings like this:

string s = "a bunch of words";
string[] parts = s.Split(' '); //Splits the string where ever there is a space character


After that you could for example parse the string array to another array in reverse, or randomly, and reconstruct the new string from that.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900