3 Replies - 4868 Views - Last Post: 11 November 2009 - 08:33 AM Rate Topic: -----

#1 Jer5078  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-November 09

Count occurences of letters in a string

Posted 04 November 2009 - 07:32 AM

Been working on this code for quite some time now and can't seem to figure out what to do.

Here is the problem:

Quote

Write a Visual Basic Console Application which executes a Do-While loop repetitively. Each time the Do-While loop executes input a String value, count and display the number of occurrences of the letters ‘a’, ‘b’, ‘c’, ‘d’ and ‘e’. Use at least one Select Case statement inside your Do-While loop. For example, suppose during one execution of the Do-While loop you input the following String value: “My mother is a great lady!”. Your program should input this value into a String variable, compute the number of times each of the letters ‘a’, ‘b’, ‘c’, ‘d’ and ‘e’ occurs and output the results: a – 3, b – 0, c – 0, d – 1, and e – 2. Terminate your Do-While loop once a value of “All Done!” is entered for the String value.


Here is my code:

  Dim s1 As String
		Dim a, b, c, d, e, count As Integer

		Do While (s1 <> "all done")
			Console.WriteLine("Please enter a sentence")

			Select Case s1.Chars(count)
				Case "a"
					a = a + 1
				Case "b"
					b = b + 1
				Case "c"
					c = c + 1
				Case "d"
					d = d + 1
				Case "e"
					e = e + 1

			End Select
			Console.WriteLine("A = " & a & vbNewLine & "B = " & b & vbNewLine & "C = " & c & vbNewLine & "D = " & d & vbNewLine & "E = " & e)

		Loop


This post has been edited by Jer5078: 04 November 2009 - 01:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Count occurences of letters in a string

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Count occurences of letters in a string

Posted 04 November 2009 - 08:15 AM

1) You need a
s1 = Console.Readline()
to get the user's input
2) You should set the a..e Integers to zero instead of relying on default behaviour - it also means you don't reset it between sentences...
3) You need an inner For loop to loop over the characters in the input string
For count As Integer = 0 To s1.Length - 1
  Select Case s1(count)


Was This Post Helpful? 1
  • +
  • -

#3 Jer5078  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-November 09

Re: Count occurences of letters in a string

Posted 09 November 2009 - 07:13 AM

Hrm... put in the For loop like you suggested and now I'm just getting an error. I'm kind of stuck here, have no idea what to do. The error says Variable 'count' hides a variable in an enclosing block.

Here is my most recent code

Module Module1

	Sub Main()
		Dim s2, s1 As String
		Dim a, b, c, d, e, count As Integer

		Console.WriteLine("Please enter a sentence")
		s1 = Console.ReadLine()

		Do While (s1 <> "all done")

			For count As Integer = 0 To s1.Length - 1

				Select Case s1(count)
					Case "a"
						a = a + 1
					Case "b"
						b = b + 1
					Case "c"
						c = c + 1
					Case "d"
						d = d + 1
					Case "e"
						e = e + 1

				End Select
			Next

			Console.WriteLine("A = " & a & vbNewLine & "B = " & b & vbNewLine & "C = " & c & vbNewLine & "D = " & d & vbNewLine & "E = " & e)

			Console.WriteLine("Please enter a sentence")
			s1 = Console.ReadLine()

		Loop

		s2 = "Hi I hate today"
		Console.WriteLine(s2)

		s2 = s2.Replace("hi", "hello")
		s2 = s2.Replace("hate", "love")
		s2 = s2.Replace("today", "tomorrow")

		Console.WriteLine(s2)

	End Sub

End Module



Btw, ignore everything after the loop, that is for the 2nd part of the assignment.
Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Count occurences of letters in a string

Posted 11 November 2009 - 08:33 AM

You declare a variable called count at the top of the method:
		Dim a, b, c, d, e, count As Integer

and then you declare ANOTHER variable called count in the For loop:
			For count As Integer = 0 To s1.Length - 1


You need to remove one of the declarations e.g. change the For loop to:
			For count = 0 To s1.Length - 1

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1