Help with Visual Basic 5.0 Assignment: Reverse Text Case

  • Thread starter GluonZ
  • Start date
  • Tags
    Visual
In summary: 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.
  • #1
GluonZ
44
0
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.

Example:

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.
 
Technology news on Phys.org
  • #2
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:
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)
	else
		'char is lowercase
		newstr = newstr + UCase(char)
	end if
next

msgbox(newstr)
 
  • #3
Ah, that makes sense. Thanks man.
 
  • #4
GluonZ, if you don't mind i want to to borrow this thread to ask a question...
well, are they anybody know how to save .dat files in VB 6.0 c source?
 
  • #5
1016 said:
GluonZ, if you don't mind i want to to borrow this thread to ask a question...
well, are they anybody know how to save .dat files in VB 6.0 c source?

.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...
 
  • #6
Ok, I'm back with another question.

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.
 
  • #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.
 
  • #8
Gluonz, it doesn't sound like you know what an array is. Perhaps it's time to refresh your knowledge. Do that.
 
  • #9
A dictionary is actually a better fit to this problem than an array.
 
  • #10
After reading Jheriko's post and some other help I did something like this

Code:
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:
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:
  • #11
You can just use your own Replace function:
Code:
Function Replace(originalString, matchChar, replaceWith)
	str = ""
	for i=1 to Len(originalString)
		char = Mid(originalString, i, 1)
		if matchChar = char then
			str = str & replaceWith
		else
			str = str & char
		end if
	next
	Replace = str
End Function
 
  • #12
GluonZ said:
After reading Jheriko's post and some other help I did something like this

Code:
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:
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.

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!
 
  • #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...

Anyway

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

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

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
Next

out = ""

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

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]
 

What is Visual Basic 5.0?

Visual Basic 5.0 is a programming language and development environment created by Microsoft. It is often used for creating graphical user interfaces and applications for the Windows operating system.

What is the purpose of "Reverse Text Case" in Visual Basic 5.0?

"Reverse Text Case" is a function in Visual Basic 5.0 that allows you to change the case of a string from upper to lower or vice versa. This can be useful for manipulating text in various ways within a program.

How do I use the "Reverse Text Case" function in Visual Basic 5.0?

To use the "Reverse Text Case" function, you will need to first declare a string variable and assign it a value. Then, you can use the "UCase" function to convert the string to all uppercase letters or the "LCase" function to convert it to all lowercase letters.

Can I use "Reverse Text Case" on a specific part of a string?

Yes, you can use the "Mid" function in combination with the "UCase" or "LCase" function to change the case of a specific portion of a string. This allows for more precise manipulation of text within a program.

Are there any limitations to using the "Reverse Text Case" function in Visual Basic 5.0?

One limitation to keep in mind is that the "Reverse Text Case" function does not work with non-alphabetic characters, such as numbers or symbols. It will only change the case of letters in a string. Additionally, the function may have limited compatibility with newer versions of Visual Basic.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Advanced Physics Homework Help
Replies
6
Views
1K
Replies
2
Views
863
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
Replies
46
Views
2K
Replies
2
Views
1K
  • Special and General Relativity
Replies
6
Views
1K
Replies
22
Views
2K
Back
Top