|
-
Apr 13th, 2009, 09:13 PM
#1
Thread Starter
Frenzied Member
Replace 5th vowel
Hey,
I need to find the first letter in a message and replace it with the 5th vowel in another string. However it never gets into the condition in Red Color. Any help please ?
I also need to keep the count of the characters in the characterarray. ???
Code:
Private Sub Find5vowel()
Dim vowelcounter, i As Integer
Dim characterarray() As Char
Dim vowelarray() As Char = {"a", " A", "e", "i", "I", "o", "O", "u", " U", "E"}
Dim strmessage1 As String
'RandomGen1 is a string of 200 letters which has been randomized
characterarray = RandomGen1.ToCharArray()
For i = 1 To characterarray.Length
If vowelarray = characterarray(i) Then
vowelcounter += 1
If vowelcounter = 5 Then
letter5vowel = characterarray(i)
strmessage1 = characterarray.ToString
strmessage1 = strmessage1.Replace(letter5vowel, firstmessage)
End If
End If
Next
MessageBox.Show(strmessage1)
End Sub
Private Sub Findfirst()
Try
If txtmessage.Text = String.Empty Then
MessageBox.Show("Message cannot be left empty!")
Else
strmessage = txtmessage.Text
Dim messagearray() As Char
messagearray = strmessage.ToCharArray()
firstmessage = messagearray(0)
' MessageBox.Show(firstmessage)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Findfirst()
Find5vowel()
End Sub
Last edited by angelica; Apr 13th, 2009 at 09:17 PM.
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Apr 13th, 2009, 09:17 PM
#2
Re: Replace 5th vowel
Have you stepped through your code? The only reason that execution won't enter an If block is because the condition evaluates to False. If you step through your code and examine the values of your variables at each step then it should be fairly obvious why an expression evaluates to False.
-
Apr 13th, 2009, 09:22 PM
#3
Re: Replace 5th vowel
Just a tip, but you can shorten your vowelarray to only uppercase and just use .ToUpper() in your code, like:
Code:
If vowelarray = characterarray(i).ToUpper() Then
That way it will run faster. Also, if you just store all the vowels to a string, like this:
Code:
Dim vowelString as String = "AEIOUY"
Then you can just use:
Code:
If vowelString.Contains(characterarray(i).ToUpper())
So you don't even need to loop through the vowel array, just use .contains to see if it has a vowel.
-
Apr 13th, 2009, 09:24 PM
#4
Thread Starter
Frenzied Member
Re: Replace 5th vowel
Yes I did and plenty of vowels have been generated (out of 200!) so there must be something else.
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Apr 13th, 2009, 09:34 PM
#5
Thread Starter
Frenzied Member
Re: Replace 5th vowel
Heya Vectris,
Code:
If vowelstring.Contains(characterarray(i).ToUpper())
get an error there! ToUpper does not accept the number of arguments
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Apr 13th, 2009, 09:47 PM
#6
Re: Replace 5th vowel
huh, I didn't test it but I never remember ToUpper having any parameters. Try declaring a string, store the characterarray(i) to the string, then use .ToUpper() on the string and put that inside the ( ) for contains.
-
Apr 13th, 2009, 10:05 PM
#7
Re: Replace 5th vowel
 Originally Posted by angelica
Heya Vectris,
Code:
If vowelstring.Contains(characterarray(i).ToUpper())
get an error there! ToUpper does not accept the number of arguments
characterarray(i) is presumably a Char and Char.ToUpper is a Shared method, not an instance method, and it takes a single Char as an argument, i.e. the character you want to convert to upper case.
-
Apr 13th, 2009, 10:14 PM
#8
Thread Starter
Frenzied Member
Re: Replace 5th vowel
Heya,
Thanks for the last clue Vectris. Looks like I need some more help please.
I have changed some code as Vetris suggested and is working fine now But
the strmessage1 is not returning a string with the alterations as I expected but gives a message with "System Char []
...............
Dim strmessage1 As String
'RandomGen1 is a string of 200 letters which has been randomized
characterarray = RandomGen1.ToCharArray()
For i = 1 To characterarray.Length
If vowelstring.Contains(characterarray(i).ToString.ToUpper()) Then
vowelcounter += 1
If vowelcounter = 5 Then
letter5vowel = characterarray(i)
strmessage1 = characterarray.ToString
strmessage1 = strmessage1.Replace(letter5vowel, firstmessage)
Exit For
End If
End If
Next
MessageBox.Show(strmessage1)
End Sub[/CODE]
PS
Could it be that the :
If vowelstring.Contains(characterarray(i).ToString.ToUpper()) Then
is logically incorrect. I need to check whether characterarry contains and vowels (vowelstring) not the other way round,but when I tried to twist it round I still got an empty message
Last edited by angelica; Apr 13th, 2009 at 10:29 PM.
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Apr 13th, 2009, 10:18 PM
#9
Re: Replace 5th vowel
check this line
Code:
strmessage1 = characterarray.ToString
You forgot the index 
Code:
strmessage1 = characterarray(i).ToString
EDIT: Wait, what you trying to do here?
Code:
strmessage1 = characterarray.ToString
strmessage1 = strmessage1.Replace(letter5vowel, firstmessage)
The first line stores something to strmessage1, but the second line will store right over it making that first line useless.
Also, you never declare letter5vowel, where is that part? Same for firstmessage.
Last edited by Vectris; Apr 13th, 2009 at 10:21 PM.
-
Apr 13th, 2009, 10:29 PM
#10
Re: Replace 5th vowel
I'm guessing that you were trying to display the random 200 letter word with the 5th vowel replaced with whatever firstmessage was (I'm also assuming that letter5vowel is a String, if not, you might have to use the .ToString() method). In that case change this part:
Code:
letter5vowel = characterarray(i)
strmessage1 = characterarray.ToString
strmessage1 = strmessage1.Replace(letter5vowel, firstmessage)
to
Code:
letter5vowel = characterarray(i)
strmessage1 = RandomGen1.Replace(letter5vowel, firstmessage)
But just to let you know, this will replace every vowel in the entire word that matches letter5vowel. So if the 5th vowel is "e", then no just the 5th "e" will be replaced, but all of the "e"s will be. To remove just the 5th e you need to record the index of the 5th value, just store your i variable to an integer called fifthIndex or something and then I think you can use the parameters of Replace for where to start and for how many characters to replace starting from that position.
-
Apr 13th, 2009, 10:53 PM
#11
Thread Starter
Frenzied Member
Re: Replace 5th vowel
Heya,
I am not sure how to use the index. Not quite familiar with the syntax here. I have tried the code below but it seems it is replaces all occurances of the same vowel.Can someone pls tell what Im doing wrong?
Dim index5th As Integer = RandomGen1.IndexOf(characterarray(i))
strmessage1 = RandomGen1.Replace(RandomGen1(index5th), firstmessage)
Last edited by angelica; Apr 14th, 2009 at 04:11 AM.
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Apr 14th, 2009, 04:12 AM
#12
Thread Starter
Frenzied Member
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Apr 14th, 2009, 10:27 AM
#13
Re: Replace 5th vowel
If this isn't homework you could use this:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim RandomStr As String = "KGNJHBGBHASDJGHABWEKWHGLHEABNGLWEABGLAEWHBGHABGAJWKEBHGLAWHEGBLAEVBALENBLGABEWGLEGBALGWHEOIQTUIWEHGJKEBGNMZX"
Dim Vowels As String = "AEIOU"
Dim VowelNum, Index As Integer
Do While VowelNum < 5 And Index <> RandomStr.Length
If Vowels.Contains(RandomStr.Substring(Index, 1).ToUpper) Then
VowelNum += 1
End If
Index += 1
Loop
If VowelNum = 5 Then
MsgBox("Fifth vowel: " & RandomStr.Substring(Index - 1, 1) & " was char number " & Index & " in the string.")
Else
MsgBox("There is no fifth vowel.")
End If
End Sub
This just finds the fifth vowel, you will still have to replace it in your message. (I didn't want to steal all the fun)
-
Apr 14th, 2009, 01:15 PM
#14
Thread Starter
Frenzied Member
Re: Replace 5th vowel
hey ,
The above code gives a NullReferenceException. ???
I need some help in the replacing please.
Last edited by angelica; Apr 14th, 2009 at 02:49 PM.
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Apr 14th, 2009, 03:50 PM
#15
Re: Replace 5th vowel
Let's go one step at a time (replacing later). Be specific about where the above code gives an Exception. Post your code showing how you are using it... because it works fine for me.
-
Apr 14th, 2009, 11:15 PM
#16
Thread Starter
Frenzied Member
Re: Replace 5th vowel
Okay ,
I was not refering to my RandomGenerator in your code correctly. Now it is working fine.
Now I need to replace the RandomGenerator (200 char) + the index of the 5th vowel in the 2nd block of the same index generator (same RandomGenerator. Below is your code with my altertions
Code:
RandomGenerator1()
Dim RandomStr As String = RandomGen1
Dim Vowels As String = "AEIOU"
Dim VowelNum, Index As Integer
Do While VowelNum < 5 And Index <> RandomStr.Length
If Vowels.Contains(RandomStr.Substring(Index, 1).ToUpper) Then
VowelNum += 1
End If
Index += 1
Loop
If VowelNum = 5 Then
MsgBox("Fifth vowel: " & RandomStr.Substring(Index - 1, 1) & " was char number " & Index & " in the string.")
Else
MsgBox("There is no fifth vowel.")
End If
End Sub
Thanks for your help!
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|