Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / November 2004



Tip: Looking for answers? Try searching our database.

Recursion - change chars in Upper or Lower

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GysAnn - 23 Nov 2004 16:11 GMT
Hello,

I'm looking for a function for converting the input to a regular text.
Example: when the input is "aBCdefgHI     jkLM ooPP" the return value of the
function should be"Abcdefghi Jklm Oopp"

Thanks in advance,

Anita
Rick Rothstein - 23 Nov 2004 16:27 GMT
> I'm looking for a function for converting the input to a regular text.
> Example: when the input is "aBCdefgHI     jkLM ooPP" the return value of the
> function should be"Abcdefghi Jklm Oopp"

Give this a try...

TextString = "aBCdefgHI     jkLM ooPP"
' Uppercase the first letter of each word
TextString = StrConv(TextString, vbProperCase)
' Remove multiple spaces inside the text
Do While InStr(TextString, "  ")
 TextString = Replace(TextString, "  ", " ")
Loop

Rick - MVP
GysAnn - 23 Nov 2004 16:31 GMT
Hi Rick,
Thanks for the quick answer, but it must be a function (functionCorrectText
by example) that checks every character at a time in an recursive way.

Anita
>> I'm looking for a function for converting the input to a regular text.
>> Example: when the input is "aBCdefgHI     jkLM ooPP" the return value
[quoted text clipped - 12 lines]
>
> Rick - MVP
Rick Rothstein - 23 Nov 2004 16:43 GMT
> Thanks for the quick answer, but it must be a function (functionCorrectText
> by example) that checks every character at a time in an recursive way.

Recursion? That seems like an awfully inefficient way to attack this
problem. Is this a homework assignment by any chance in which your
instructor is trying to teach you a certain technique? If so, is that
technique "recursion", or something else?

Rick - MVP
GysAnn - 23 Nov 2004 17:36 GMT
Yes, I'm learning different programming techniques by myself, and recursion
is one of it; it should look like this (+ -)

myString = "aBCdefgHI     jkLM ooPP"    '(from InputBox by example)
myCorrectedStr =  fncCorrectInput(myString)

Private Function fncCorrectInput(ByVal myString as String) As String
   If ....
       Return....
   Else
       Return .....(?) ......  & fncCorrectInput(myParameter)
   End If
End Function

>> Thanks for the quick answer, but it must be a function
> (functionCorrectText
[quoted text clipped - 6 lines]
>
> Rick - MVP
Rick Rothstein - 23 Nov 2004 17:59 GMT
> Yes, I'm learning different programming techniques by myself, and recursion
> is one of it; it should look like this (+ -)
[quoted text clipped - 9 lines]
>     End If
> End Function

Sorry, I can't help you... in some 25 years of programming, I have never
found a need to use recursion. Anything you can do with recursion can be
done using two functions with one calling the other repeatedly... and
doing this will result in clearer, easier to understand (say, a year
from now when you have to maintain it) code. And more than likely, as in
this case, the functionality that recursion seems necessary for can
simply be done in another way altogether.

Rick - MVP
Steve Gerrard - 24 Nov 2004 02:58 GMT
| Sorry, I can't help you... in some 25 years of programming, I have never
| found a need to use recursion. Anything you can do with recursion can be
[quoted text clipped - 3 lines]
| this case, the functionality that recursion seems necessary for can
| simply be done in another way altogether.

Gotta disagree with that. Recursion can be useful when it used right
(this case is not a good candidate). The classic example is the
QuickSort algorithm. Mike Williams recently re-posted another classic
use, that of recursively listing the files and folders on a drive (using
the File APIs).

Recursion lets you make use of the stack implemented in most (all?)
processors. This is more efficent than allocating arrays to store all
the temporary values you need without recursion (and more efficent than
writing your own stack).

I would claim that a quick sort implementation that does not use
recursion would be harder to understand and more difficult to maintain
than one that uses recursion.
Rick Rothstein - 24 Nov 2004 04:48 GMT
> | Sorry, I can't help you... in some 25 years of programming, I have
> never
[quoted text clipped - 21 lines]
> recursion would be harder to understand and more difficult to maintain
> than one that uses recursion.

Well, maybe, and I emphasize "maybe"<g>, I'll give you something like
the QuickSort algorithm; but probably for efficiency, not clarity (in my
opinion, of course). Perhaps it is just a blind spot with me, but I find
trying to unraveling a deep recursion (and you have to watch out you
don't go too deep with recursion algorithms), say during debugging or
while designing the routine using it, an awfully challenging chore. Give
me one function repeatedly calling a companion function (what almost all
recursions can be unwound into) and I can follow what is going on with
almost no thought. As I said, perhaps it is just me.

Rick
Jim Mack - 25 Nov 2004 11:40 GMT
>>> Sorry, I can't help you... in some 25 years of programming, I have
>>> never found a need to use recursion. Anything you can do with
[quoted text clipped - 37 lines]
>
> Rick

Don't even give him that one :-)

It's well-proven that not only is recursion never necessary, it is
almost never as efficient as the alternative.  The only thing it gives
you is clarity -- a 'depth search' of the file system is the perfect
example.  The disk I/O overhead is so great that the algorithm hardly
matters, so why not go for simplicity.

In the case of quicksort, given the best code in each case, a
non-recursive approach will beat a recursive one, especially in a system
where the overhead of a function call and passing parameters is high.

Signature

       Jim

Rick Rothstein - 25 Nov 2004 14:42 GMT
> >>> Sorry, I can't help you... in some 25 years of programming, I have
> >>> never found a need to use recursion. Anything you can do with
[quoted text clipped - 39 lines]
>
> Don't even give him that one :-)

Okay, I won't.<g>

> It's well-proven that not only is recursion never necessary,

That part I'm aware of, although I hadn't realized it was proven (I'm
not, and never have been, a student of recursion).

> it is almost never as efficient as the alternative.

See, it is that "almost" that I thought applied to the QuickSort
algorithm (but as you say later, this is not the case).

> The only thing it gives you is clarity

I honestly never thought a recursive routine was ever clearer than its
non-recursive alternative... I just find it near impossible to figure
out where I am in one of those things while trying to debug a problem
during development.

> -- a 'depth search' of the file system is the perfect
> example.  The disk I/O overhead is so great that the algorithm hardly
[quoted text clipped - 3 lines]
> non-recursive approach will beat a recursive one, especially in a system
> where the overhead of a function call and passing parameters is high.

Rick
Jim Mack - 25 Nov 2004 16:48 GMT
>> The only thing it gives you is clarity
>
> I honestly never thought a recursive routine was ever clearer than its
> non-recursive alternative... I just find it near impossible to figure
> out where I am in one of those things while trying to debug a problem
> during development.

If a function isn't easier to grasp when done recursively, then there's
no reason to use recursion, because simplicity and clarity are just
about all it offers.

I can see getting lost in the depths while debugging.  A static counter
that tracks your progress can be invaluable there.

Signature

       Jim

Rick Rothstein - 25 Nov 2004 17:23 GMT
> >> The only thing it gives you is clarity
> >
[quoted text clipped - 9 lines]
> I can see getting lost in the depths while debugging.  A static counter
> that tracks your progress can be invaluable there.

Nah! I think I will just stick to what I have been doing for the last 25
or so years and just not use recursion in any of my programs. ;-)

Rick
Bert Byfield - 25 Nov 2004 19:20 GMT
> Nah! I think I will just stick to what I have been doing for the
> last 25
> or so years and just not use recursion in any of my programs. ;-)

Recursion is normally overkill, but here is a case where it solves a real
problem, that of generating n permutations of a string's characters (e.g.:
for ABC generate: ABC, ACB, BCA, BAC, CAB, CBA). Note that it will work for
most any number of characters (until you run out of memory or crash
something in that category), but it takes forever after 9 or 10.

------- cut here ---------------------------------------------

'To test, make a form and define everything not defined by the
' following form code:

Option Explicit

' Description:Takes in a string and writes out all possible
' permutations of the inputted characters to a file
' using a simple recursive routine.

Private Sub Form_Load()
   txtArg.Text = ""         'clear input text
   lblStatusMessage.Caption = "Enter Letters to Permutate"
End Sub

Private Sub cmdDoIt_Click()

   Dim s As String, s1 As String, i As Integer
   
   s = UCase$(txtArg.Text)      'check the input
   
   If Len(s) > 10 Then
       MsgBox "Only 10 letters allowed as input for this test", , "FIX
LETTERS"
       Exit Sub
   End If
   
   For i = 1 To Len(s)
       s1 = Mid$(s, i, 1)
       If s1 < "A" Or s1 > "Z" Then
           MsgBox "All input text for this test must be letters", , "INPUT
ERROR"
           Exit Sub
       End If
   Next i
   
   lblStatusMessage.Caption = "Working... Please wait..."
   
   Open App.Path & "\words.txt" For Output As #1
   PermutateToFile s, ""    'get all permutations of given letter group
   Close #1
   
   lblStatusMessage.Caption = "DONE"
   
   'Display the results (3,628,800 lines and 42 megs for 10 letters)
   Shell "notepad " & App.Path & "\words.txt", vbNormalFocus

End Sub

Private Sub cmdExit_Click()
   Unload Me
End Sub

'*********** PROCEDURES **************************************

'Recursive Procedure to generate all permutations of a string
'  and write them to a file
Private Sub PermutateToFile(ByVal LETTERS_IN As String, _
                       ByVal TEXTBUILT_IN As String)
   Dim i As Integer
   
   If Len(LETTERS_IN) = 1 Then
       Print #1, TEXTBUILT_IN & LETTERS_IN
       Exit Sub
   End If
   
   For i = 1 To Len(LETTERS_IN)
       PermutateToFile Mid(LETTERS_IN, 1, i - 1) & Mid(LETTERS_IN, i + 1),
_
                       TEXTBUILT_IN & Mid(LETTERS_IN, i, 1)
   Next i

End Sub
J French - 26 Nov 2004 08:57 GMT
<snip>

>Nah! I think I will just stick to what I have been doing for the last 25
>or so years and just not use recursion in any of my programs. ;-)

Funny that, I quite often find that recursive solutions are the
simplest.

Obviously only where they are appropriate
- to use rucursion when it is inappropriate is just plain daft
Frank Adam - 25 Nov 2004 21:37 GMT
>>> The only thing it gives you is clarity
>>
[quoted text clipped - 6 lines]
>no reason to use recursion, because simplicity and clarity are just
>about all it offers.

Funny, i've intentionally used quite some recursion before i've picked
up VB and i can't even remember one thing to justify it with.. dammit
and i so wanted to argue today. :)

>I can see getting lost in the depths while debugging.  A static counter
>that tracks your progress can be invaluable there.

I think that's a VB feature though. I can't recall ever getting lost
in C. Matter of fact it was probably the easiest thing to follow in
the whole application, considering how we've 'modulized' code to death
in the name of portability. Good ol' days. :)

Signature

Regards, Frank

Jim Mack - 25 Nov 2004 23:59 GMT
> dammit and i so wanted to argue today. :)

No you didn't.

Signature

       Jim

Mike Williams - 26 Nov 2004 11:45 GMT
> No you didn't.

Yes he did. He told me so yesterday!
Dag Sunde - 26 Nov 2004 00:02 GMT
<snipped/>

> I think that's a VB feature though. I can't recall ever getting lost
> in C. Matter of fact it was probably the easiest thing to follow in
> the whole application, considering how we've 'modulized' code to death
> in the name of portability. Good ol' days. :)

"good ol' days" and "C" just reminded me of an expression from way back
when...

"To Iterate is Human, to Recurse Divine..."

;-)

Signature

Dag.

preben nielsen - 23 Nov 2004 21:16 GMT
> Yes, I'm learning different programming techniques by myself,
> and recursion is one of it; it should look like this (+ -)
>
> myString = "aBCdefgHI     jkLM ooPP"    '(from InputBox by
> example)
> myCorrectedStr =  fncCorrectInput(myString)

Well...... This reverses the Case of every letter in the string
recursively. It shows you how to run through the string. If you
need to uppercase only the first letter of each word then you have
to expand it yourself (not difficult).

Private Function fncCorrectInput(ByVal myString As String) As
String
 Dim strTmp As String
 If myString = "" Then Exit Function
 strTmp = Mid(myString, 1, 1)
 If LCase(strTmp) = strTmp Then
   strTmp = UCase(strTmp)
 Else
   strTmp = LCase(strTmp)
 End If
 fncCorrectInput = strTmp & fncCorrectInput(Mid(myString, 2))
End Function

Private Sub Form_Load()
 Dim myString
 myString = "aBCdefgHI     jkLM ooPP"
 MsgBox fncCorrectInput(myString)
End Sub

Signature

/\ preben nielsen
\/\ prel@post.tele.dk

GysAnn - 23 Nov 2004 21:59 GMT
Thanks Preben !!!! You put me on the right track !!!

> Yes, I'm learning different programming techniques by myself, and
> recursion is one of it; it should look like this (+ -)
[quoted text clipped - 20 lines]
>>
>> Rick - MVP
GysAnn - 27 Nov 2004 13:20 GMT
Hello everyone,

Thanks to all who realy understud my question: it whas an item of a task for
school, thus not to open an discusion concering the good or bad things on
recursion. It was only to learn the technique, not to discuse the benefits
of it :-).

Also thanks to all who put me on the right track !

I've completed my task, when I get it back with the teachers remarks I'll
post the code here.

Again... Thanks to you all, and greetings from bElGiUm :-)

Anita

>> I'm looking for a function for converting the input to a regular text.
>> Example: when the input is "aBCdefgHI     jkLM ooPP" the return value
[quoted text clipped - 12 lines]
>
> Rick - MVP
Rick Rothstein - 27 Nov 2004 15:29 GMT
> Thanks to all who realy understud my question: it whas an item of a task for
> school, thus not to open an discusion concering the good or bad things on
[quoted text clipped - 3 lines]
> I've completed my task, when I get it back with the teachers remarks I'll
> post the code here.

The reason you ended up with the discussion regarding the good or bad
things about recursion is because you apparently lied to us. I asked you
earlier if this was a homework assignment for school and you replied...

    "I'm learning different programming techniques
     by myself, and recursion is one of it."

This pretty much said "No, it is not a homework assignment", so we all
attempted to tell you about the evils of recursion. Now, however, you
say your question was asked because it was a "task for school" and you
will even tell us your "teacher's remarks" when you get them. We spent a
lot of time here trying to guide you and all you seemed to be looking
for is someone to do your homework for you. That was not very nice
Anita.

Rick
GysAnn - 30 Nov 2004 21:25 GMT
Hello Rick,

The complete task (Remove multiple spaces, Invallid char's, Bubble sort,
etc) was more than I asked for in this thread and we have to made it with
the use of itiration. I made by meself the goal to made it recursive en will
give my solution to the teacher to evaluate for my personal idea ... and ...
no one have give me an ready copy-and-paste solution, therefore I specialy
not mentioned all the functions of the homework assignment ; this thread was
only mentioned to put me on the right track...

And I have the luck (!) that there are many answers to this thread, so I've
learned a lot about the pro- and contras in this mather; and therefore I
thank you all ! Now I can partisipate in a discusion about recursion with a
lot of background information.

Thanks again,
Anita

>> Thanks to all who realy understud my question: it whas an item of a
> task for
[quoted text clipped - 24 lines]
>
> Rick
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.