String manipulation
Author |
Message |
isaiahk9
|
Posted: Tue Nov 11, 2008 4:19 pm Post subject: String manipulation |
|
|
Hey, I?ve got some questions about string manipulation. If any of these could get solved, I would be grateful. So, without further ado, here they are :
1. This is a program that displays the number of times a specific letter occurs in a word or phrase. After declaring all variables/assigning them initial values, and checking that all the strings entered are OK with the program, I have this section of code :
Do
' The variable blnExists is made true or false, saying that if the letter appears, then it is true
blnExists = InStr(intRunTimes, txtEnterSentence.Text, txtEnterLetter.Text) <> 0
' The program makes it so that the next time it runs the program, it will check for the next occurence
intRunTimes = intRunTimes + 1
' Adds one correct to your score
intCorrect = intCorrect + 1
Loop While blnExists = True
MsgBox "The number of times '" & txtEnterLetter.Text & "' appeared was " & intCorrect - 1 & "."
However, it gives me huge numbers. For the sentence ?Enter yours sentence to search the letter for.?, the program showed 31 too many e?s, 27 too many a?s, and 22 too many s?s. Any idea why?
2. This is a different program that allows the user to enter his or her first and last names and then displays the initials of the name in upper case. What string manipulating functions would I need for that program?
Thanx |
|
|
|
|
|
Sponsor Sponsor
|
|
|
OneOffDriveByPoster
|
Posted: Tue Nov 11, 2008 9:47 pm Post subject: Re: String manipulation |
|
|
VisualBASIC: | blnExists = InStr(intRunTimes, txtEnterSentence.Text, txtEnterLetter.Text) <> 0 | It would help if you stored the value returned by InStr.
VisualBASIC: | ' The program makes it so that the next time it runs the program, it will check for the next occurence
intRunTimes = intRunTimes + 1 | This does not do what the comment says it does. The name intRunTimes indicates a logic error. |
|
|
|
|
|
isaiahk9
|
Posted: Wed Nov 12, 2008 5:50 am Post subject: RE:String manipulation |
|
|
OK . . .
does'nt the blnExists store the value returned by InStr (blnExists's a variable)? And yeah, that code should do what it is supposed to. It is supposed to look for the intRunTimes-nth occurence of EnterLetter. That way, it continues checking the sentence until it has been checked as many times as needed for all the occurences to be found. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Wed Nov 12, 2008 8:46 am Post subject: Re: RE:String manipulation |
|
|
isaiahk9 @ Wed Nov 12, 2008 5:50 am wrote: does'nt the blnExists store the value returned by InStr (blnExists's a variable)? No. You have a comparison that takes the return value. I assume blnExists is a Boolean. InStr does not return a Boolean.
isaiahk9 @ Wed Nov 12, 2008 5:50 am wrote: And yeah, that code should do what it is supposed to. It is supposed to look for the intRunTimes-nth occurence of EnterLetter. That way, it continues checking the sentence until it has been checked as many times as needed for all the occurences to be found. No. You are looking at the next letter in the input string. This letter may not be an instance of the letter that you are looking for. |
|
|
|
|
|
isaiahk9
|
Posted: Wed Nov 12, 2008 4:58 pm Post subject: RE:String manipulation |
|
|
Yeah, but when there is the <> 0 at the end, it becomes true or false. For instance, run VB and in the form load procedure go like this :
Dim blnTrueorFalse As Boolean
blnTrueorFalse = InStr("Ketchup", "up") <> 0
MsgBox "" & blnTrueorFalse
Trust me, I've tried it.
As for your second point, are we on different versions of Vb? I'm on VB 6.0 (not enterprise). In VB 6.0, that number does not mean the next letter, but the next occurrence of the letter. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Wed Nov 12, 2008 5:19 pm Post subject: Re: RE:String manipulation |
|
|
isaiahk9 @ Wed Nov 12, 2008 4:58 pm wrote: As for your second point, are we on different versions of Vb? I'm on VB 6.0 (not enterprise). In VB 6.0, that number does not mean the next letter, but the next occurrence of the letter. I'm using the reference at http://msdn.microsoft.com/en-us/library/8460tsh1(VS.80).aspx. |
|
|
|
|
|
isaiahk9
|
Posted: Wed Nov 12, 2008 6:23 pm Post subject: RE:String manipulation |
|
|
That's our communications problem. my techniques should work in VB 6.0, but your reference is for a different edition. thanx anyways, I'm near a VB expert tomorrow anyhow. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Wed Nov 12, 2008 7:23 pm Post subject: Re: RE:String manipulation |
|
|
isaiahk9 @ Wed Nov 12, 2008 6:23 pm wrote: That's our communications problem. my techniques should work in VB 6.0, but your reference is for a different edition. thanx anyways, I'm near a VB expert tomorrow anyhow. The VB 6.0 reference indicates the same (the `start' parameter is not to look for the n-th occurrence, but from the n-th character): http://msdn.microsoft.com/en-us/library/aa445031.aspx |
|
|
|
|
|
Sponsor Sponsor
|
|
|
isaiahk9
|
Posted: Wed Nov 12, 2008 8:30 pm Post subject: RE:String manipulation |
|
|
Whoa. That's weird.
Oh! ~An epiphany!~
The one example of this that I was shown was like this :
InStr("hellohello", "hello")
Which gave me 6. The way it was shown to me was kinda misleading. So, that's the problem. thanx OneOffDriveByPoster-oh wait. Do you have any insights of how to fix this problem? |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Wed Nov 12, 2008 10:42 pm Post subject: Re: RE:String manipulation |
|
|
isaiahk9 @ Wed Nov 12, 2008 8:30 pm wrote: Which gave me 6. The way it was shown to me was kinda misleading. So, that's the problem. thanx OneOffDriveByPoster-oh wait. Do you have any insights of how to fix this problem? Yes, InStr returned the position where it found the search string. If you take that and add one... |
|
|
|
|
|
isaiahk9
|
Posted: Thu Nov 13, 2008 5:40 am Post subject: RE:String manipulation |
|
|
okay, thanx. So I could create a local variable for the last number, and that would be the starting point. As well, said variable would change to a higher number every iteration, until there are no more, |
|
|
|
|
|
|
|