Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Visual Basic 5.0 help.

  1. Dec 23, 2006 #1
    Now, I know VB is hated in a lot of instances and 5.0 is out of date, however it is the only computer programming course offered here. Basically, I'm trying to do an assignment for fun at home - we never have homework (as everyone does not have VB5.0 at home, even though there is the free variant) and just do it in class. I would like to get this particular program done at home however, since I have done the other two on this assignment sheet and want to learn how to do this one since he told us when we get to #3 on the assignment sheet we would need to know something new. The topic of these assignment sheets are string manipulation and list boxes. The previous two projects on this assignment were basically simple arrays/select case programs with list boxes.

    I'm not sure exactly how to approach this question as he said we need to know something new. I'm not sure what it is, but maybe someone here could help.

    The question:

    "#3. Write a program that allows the user to type in as many words as they want. Your program should display the same message that the user typed in with all the letter cases reversed. That is, all the letters that the person typed in upper case are now lower case and all the letters that the person typed in lower case are now in upper case.


    Person types in:
    Now Is The Time

    And the computer repeats back in another location:
    nOW iS tHE tIME"

    Now, I'm not sure if I absolutely need this new thing he was talking about or not - but any help is appreciated.

    Were not very far in the course, all we have done are forms (obviously), math operations, select case and if then statements, arrays, option buttons, and currently list boxes and string manipulation.

    Thanks in advance.
  2. jcsd
  3. Dec 23, 2006 #2


    User Avatar
    Science Advisor

    Well, i haven't used VB 5.0, but i've used VB 6 and VB.NET. Here's sample code using VBScript (VBScript is not VB), which should be similar. For example save this as a file of extension .vbs (in windows) and double click to run.
    I'm looping through the characters in the string the user provides, compare them with their upper case version, and then change case as necessary.
    I used InputBox and msgbox which are built in VBScript functions for displaying input and alert boxes respectively. It should give you an idea.
    Code (Text):

    Dim str, newstr, char
    str = InputBox("Please type something, anything.")
    newstr = ""

    for i=1 to Len(str)
        char = Mid(str, i, 1)
        if char = UCase(char) then
            'char is uppercase
            newstr = newstr + LCase(char)
            'char is lowercase
            newstr = newstr + UCase(char)
        end if

  4. Dec 23, 2006 #3
    Ah, that makes sense. Thanks man.
  5. Dec 31, 2006 #4
    GluonZ, if you don't mind i wanna to borrow this thread to ask a question...
    well, are they anybody know how to save .dat files in VB 6.0 c source?
  6. Jan 3, 2007 #5
    .dat file is just an extension and not a particular file type. .dat has been used in lots of games and software installers etc... Please specify an exact type of .dat file.

    Also, what is VB6.0 c source? is that Visual Basic or C code?

    In any case you need to look up file I/O functionality. Outputting to any file type is always the same, but you will have to arrange the data manually to fit the format you want to generate. This means that you will need to find out exactly how the .dat in question is used for storing data.

    In C there is fopen, fread, fprintf and so on.

    In VB there is Open, Close, Read and Write.

    If you use the msdn library to look these up (http://msdn.microsoft.com) you will find a complete specification for each function/method as well as useful links to related functions. Some of the pages have examples too.

    Generally you will want to use binary file access. Every other type is a subset of binary access anwway...

    Good luck working it out...
  7. Jan 3, 2007 #6
    Ok, I'm back with another question.


    "Write a program that will allow the user to type in as many words as they want. Your program should then display how many of each letter in the alphabet was typed in. That means how many a's did the person type in, how many b's did the person type in, how many c's did the person type in, etc. The computer should count both upper case and lower case letters as the same. Display your answer in a list box. Your program must use at least one array."

    Ok - a few things. First of all, I could already write this program using a bunch of kount's (for all the letters), but there must be some better way to do it, no? Also, maybe this is what I'm missing, but how would we fit an array into this? Whenever we had arrayed anything before we just copied a text box or something and used a simple loop. Arraying actual things on the form won't really be necessary here, so am I missing something in code?

    Now, I know I could write this program already - however I feel that there must be a better, more efficient way. An explanation or example would be of great help and greatly appreciated.

    Thanks, once again, in advance.
  8. Jan 3, 2007 #7
    What you want to do is find the ascii code for each character and use that to generate an index for an array. IIRC A is 65 and a is 97. So if a character is upper case and a letter we can subtract 64 from the ascii code and otherwise if it is still a letter we subtract 96, this generates a "1-based" array index from 1 to 26. If you need it to start at 0 just subtract 65 and 97 instead.
  9. Jan 3, 2007 #8


    User Avatar
    Homework Helper

    Gluonz, it doesn't sound like you know what an array is. Perhaps it's time to refresh your knowledge. Do that.
  10. Jan 3, 2007 #9


    User Avatar
    Science Advisor

    A dictionary is actually a better fit to this problem than an array.
  11. Jan 3, 2007 #10
    After reading Jheriko's post and some other help I did something like this

    Code (Text):
    Dim i, iCount As Integer
    Dim Text As String

    For i = 65 To 90
    Text = Text1
        Text = UCase(Text1)
        iCount = Len(Text) - Len(Char(i))
        List1.AddItem Chr(i) & " - " & iCount
    Next i
    That didn't work and I was not sure why, and I was told that I need the Replace() function so it looked like this:

    Code (Text):
    Dim i, iCount As Integer
    Dim Text As String

    For i = 65 To 90
    Text = Text1
        Text = UCase(Text1)
        iCount = Len(Text) - Len(Replace(Text, Chr(i), ""))
        List1.AddItem Chr(i) & " - " & iCount
    Next i
    Turns out that feature was implemented in VB6.0 and was not in VB5.0... any ideas how to do it without the replace function? (I would do 97 to 122 for lower case too).

    Thanks for the help again.
    Last edited: Jan 3, 2007
  12. Jan 3, 2007 #11


    User Avatar
    Science Advisor

    You can just use your own Replace function:
    Code (Text):

    Function Replace(originalString, matchChar, replaceWith)
        str = ""
        for i=1 to Len(originalString)
            char = Mid(originalString, i, 1)
            if matchChar = char then
                str = str & replaceWith
                str = str & char
            end if
        Replace = str
    End Function
  13. Jan 4, 2007 #12
    You have chosen a really elaborate way of doing this (IMO), its also slow (Len on a string runs until it finds a null in memory every single time for instance). Why not take the simpler approach and simply implement the algorithm using arrays? I am going to write a vbscript to illustrate the point... I will be back!
  14. Jan 4, 2007 #13
    It took me a while, I had to wrestle with VBscript a little. I rarely ever use it and it is different enough from VB 3/4/5/6 to give me a headache. Also my VB in general is rusty...


    Code (Text):
    Dim counts(26)
    Dim i
    Dim l
    Dim retval
    Dim s
    Dim out

    For i = 0 to 25
        counts(i) = 0

    retval = InputBox("Input text:","Testy","Some text")

    l = Len(retval)
    retval = lcase(retval)

    For i = 1 to l
        s = Asc(Mid(retval,i,1)) 'get ascii value of character
        if (s>96) and (s<123) then
            counts(s - 97) = counts(s - 97) + 1
        end if

    out = ""

    For i = 0 to 25
        out = out & Chr(i+97) & " " & counts(i) & " "

    MsgBox out
    This works as a vbscript, converting to vb6 is trivial, just at the types to the Dims and add some "next i" instead of just "next".

    More importantly though notice how the only annoying function inside the loop is mid.

    [rant]Writing this reminded me of exactly all the reasons I hate VB.. Chr, Asc, Mid... all of these are annoying features that you don't have or need in a lower level language. Direct access to bytes makes writing this sort of thing a lot cleaner....[/rant]
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Visual Basic 5.0 help.
  1. Visual Basic Help (Replies: 1)

  2. Visual Basic Help (Replies: 4)

  3. Visual Basic Help (Replies: 2)

  4. Visual Basic Help (Replies: 1)