I have an assignment where we have to a number of things such as count the vowels, words, and reverse strings. I was able to write the code for the vowels and the words but am lost on the reverse. Here is the section of the assignment the professor asked.
6) Write the code for the “Reverse” button click event. The subroutine should get the text from txtSentence and pass it to a function called ReverseString. The ReverseString function should reverse the string and return it. For example ReverseString( “ABC” ) should return “CBA”. The “Reverse” button click subroutine should then update the text in txtSentence with the reversed string. You cannot use the StrReverse function. Use .Substring to extract each letter but assemble the characters back together using concatentation so that the first character ends up as the last.
I understand the concept of what it is doing and but don't know how to start it. I have used the .substring in the past to output part of the string. Can anyone give me some advice on how to go about it or even a little example? Here is the code I have written for the vowels, words, as well as the overall defined variables so far.
Thanks again.
Code:Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim str As String Dim total As Integer total = 0 str = txtSentence.Text 'look for each vowel and add the count to total total = total + CharCount(str, "A") total = total + CharCount(str, "E") total = total + CharCount(str, "I") total = total + CharCount(str, "O") total = total + CharCount(str, "U") total = total + CharCount(str, "Y") 'display results MsgBox("Total Vowels = " & total) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim str As String Dim i, l, words As Integer str = txtSentence.Text str = LTrim(str) 'removes leading blank spaces str = RTrim(str) ' removes trailing blank spaces l = str.Length i = 0 words = 0 While (i < l) If str(i) = " " Then words = words + 1 i = i + 1 While str(i) = " " ' removes continuous blank spaces i = i + 1 End While Else i = i + 1 End If End While words = words + 1 ' adds the last word MessageBox.Show("WORDS =" & words) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim random As String = "asfdgasfdgasfdgasfd11" Dim length As Integer = Nothing length = Random.Length Console.WriteLine(random.Length) Console.WriteLine(length) Console.WriteLine() Console.WriteLine() Console.ReadLine() Dim curStart = 0 Dim loopCounter = 0 While (curStart < random.Length) Console.WriteLine(random.Substring(curStart, System.Math.Min(20, length - 20 * loopCounter))) curStart = curStart + 20 loopCounter = loopCounter + 1 End While End Sub Private Function CharCount(ByVal str As String, ByVal findCH As Char) As Integer Dim idx, Ln, cnt As Integer Dim ch As String Ln = str.Length cnt = 0 findCH = Char.ToUpper(findCH) 'make sure char is an uppercase For idx = 0 To Ln - 1 'read individual letters from string and convert to uppercase ch = CChar(str.Substring(idx, 1).ToUpper) 'convert to char If ch = findCH Then cnt = cnt + 1 'found a match increment the count End If Next Return cnt End Function Private Sub txtSentence_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSentence.TextChanged End Sub End Class




Reply With Quote
