none
VB.NET rotate a string a given number of times RRS feed

  • Question

  • Create another sub/function that will rotate a string a given number of times. Each time you rotate it, the first character is removed and put onto the back. The inputs to the sub/function would be the starting string and the number of rotations. It needs to return the rotated string. Once returned display the output on the form.

    Examples    Starting with Football

    1 rotation = ootballf

    2 rotations = otballfo

    3 rotations = tballfoo

    Any Help is appreciated. Im just a student struggling to get through the code so bear with me please.

    Thursday, November 15, 2018 5:00 PM

All replies

  • Hi

    For school work, your first contact for assistance aught to be your teacher.

    However, if you post your code which of course you have ready at hand, along with questions/issues/problems, I am sure we can help.


    Regards Les, Livingston, Scotland



    • Edited by leshay Thursday, November 15, 2018 5:51 PM
    Thursday, November 15, 2018 5:50 PM
  • Hello !

    Thank you for your kind words of wisdom & suggestions however I have already contacted my teacher but have not received a reply back, hence why I am here. Im not looking for the complete code but for guidance on how to reach the solution. Would this require a "Do While...Loop" ?

    Thursday, November 15, 2018 6:14 PM
  • Hi

    Possible but not necessarily. I would tend to use a For.... Next loop for this (assuming you want to do X number of rotations for each operation).

    Your main concern would probably be how to take one letter off  the beginning and sticking it onto the end.

    Hint: one way is to use the SubString method.

    Give it a try and post your code here (in a code block - from the toolbar)


    Regards Les, Livingston, Scotland



    • Edited by leshay Thursday, November 15, 2018 6:24 PM
    Thursday, November 15, 2018 6:20 PM
  • Take a look at StringBuilder.  You can instantiate it with a string like 'Football'. It has methods to Append characters and remove characters.

    Here is the easy part.  If you read the link you might figure out how to accomplish the rest.

        Private Function RotateString(InitStr As String, count As Integer) As String
            Dim rv As New System.Text.StringBuilder(InitStr)
            For x As Integer = 1 To count
                'if you properly manipulate rv here
                ' you'll have it (two statements)
            Next
            Return rv.ToString
        End Function


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.


    • Edited by dbasnett Thursday, November 15, 2018 6:48 PM
    Thursday, November 15, 2018 6:46 PM
  • Hope it helps you

    Try
                Dim input As String = TextBox2.Text 'Football
                Dim nooftimes As Integer = Convert.ToInt32(TextBox1.Text) '3,5....
                For i = 1 To nooftimes
                    input = (input + input.Substring(0, 1)).Remove(0, 1)
                    RichTextBox1.Text += input + System.Environment.NewLine
                Next
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try

    Friday, November 16, 2018 5:17 AM
  • [...]

     Would this require a "Do While...Loop" ?

    I do not think that you need a loop. The result can be obtained by concatenation of two parts, for example:

       3 rotations — “tball” & “foo”

    The same is in case of 11 or 19 rotations.

    The length of the second part (“foo” in this case) can be calculated using this expression:

       Dim n = number_of_rotations Mod length_of_string

    Then use string functions, such as Left and Mid, to split the string into two parts, etc.


    Friday, November 16, 2018 5:45 AM