SAM to MAS

Thread: SAM to MAS

1. sam
Senior Member
Join Date
Dec 1969
Posts
866

SAM to MAS

hi how do i convert SAM to MAS<BR>i have tried<BR>MID()<BR>LEFT()<BR>RIGHT()<BR><BR>but have not been able to do so.<BR><BR>can anyone help

2. Steve Cimino Guest

RE: SAM to MAS

There is no function (that I&#039m aware of) that will simply reverse a string. I think these topics may be a bit advanced for you at this point, but you should look at the FOR loop, and the MID function (you were close with that one.. just need to manipulate it a bit more).<BR><BR>I think you were expecting a simple function to do this for you, but looks like you&#039re going to have to do it the hard way.

3. Steve Cimino Guest

What the hey

I&#039ll just give it to you...<BR><BR>Dim x, str, rstr<BR><BR>str = "SAM"<BR><BR>x = len(str)<BR><BR>Do while x &#062; 0<BR>rstr = rstr & Mid(str, x, 1)<BR>x = x - 1<BR>Loop<BR><BR>Response.write rstr

4. sam
Senior Member
Join Date
Dec 1969
Posts
866

RE: What the hey

THANKS<BR><BR>STEVE<BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

Ummm...what is wrong with StrReverse???

It&#039s sitting there, right in the middle of all the VBS functions.<BR><BR>

6. Steve Cimino Guest

RE: Ummm...what is wrong with StrReverse???

Hey! This is a teaching lesson! No cutting in with EASY functions. The only way to do this is the longest, hardest way!<BR><BR>I knew I spoke to soon... I didn&#039t know the function existed. Reminds me of first starting out breaking down file i/o line by line, searching for carriage returns.. then I found out about Line INPUT.<BR><BR>Ah well. Thanks to bill and his "fancy functions". ;)<BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

Well, if it is a programming course...

...and if we&#039re going to use code instead of StrReverse, then shouldn&#039t we at least use an ugly recursive function call?<BR><BR>&#060;HTML&#062;&#060;BODY&#062;<BR>& LT;%<BR>Function reverseString( s )<BR>&nbsp;&nbsp;&nbsp;&nbsp;If Len(s) &LT;= 1 Then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;temp = ""<BR>&nbsp;&nbsp;&nbsp;&nbsp;Else<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp = reverseString( Mid(s,2) )<BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;reverseString = temp & Left(s,1)<BR>End Function<BR>%&#062;<BR><BR>Reverse of "this is fun" is &LT;% = StrReverse("this is fun") %&#062;&LT;BR&#062;<BR>Reverse of "this is fun" is &LT;% = reverseString("this is fun") %&#062;&LT;BR&#062;<BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR>

8. Steve Cimino Guest

Its all in the timing

Of course... sweet recursion. Check out the time differences between your recursion function and the built in StrReverse function<BR><BR>(Loop of 10000 iterations)<BR>reverseString: 3.906 seconds<BR>StrReverse: .046 seconds<BR><BR>Yeah I know, I have nothing better to do but time things like this... just curious because the one and only time I used recursion it was slow as hell and I wound up with a stack overflow, so I tend to avoid it if I can.

9. sdf
Junior Member
Join Date
Dec 1969
Posts
6

it IS a programming course...

Reverse of "glenelg" is &#060;% = reverseString("glenelg") %&#062;<BR>

10. Senior Member
Join Date
Dec 1969
Posts
96,118

VBS is especially bad...

...at recursion. Or any kind of function/procedure calls.<BR><BR>And, too, I was doing a *LOT* of really, really ugly string concatenation. (The & operator in VBS is *horribly* inefficient!)<BR><BR>I think I could write this in C/C++ and end up no more than maybe 10 to 20 times slower than a straightforward way.<BR><BR>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•