Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 15th, 2013, 09:58 AM
 ragefour
Registered User

Join Date: Jan 2013
Posts: 2
Time spent in forums: 19 m 44 sec
Reputation Power: 0
Diamond and align right using string manipulation help

i can make a diamond and align right pattern using this code:

Code:
```diamond :
a = Mid("awesome", 4, 1)
Print Space(20); a
a = Mid("awesome", 3, 3)
Print Space(18); a
a = Mid("awesome", 2, 5)
Print Space(16); a
a = Mid("awesome", 1, 7)
Print Space(14); a
a = Mid("awesome", 2, 5)
Print Space(16); a
a = Mid("awesome", 3, 3)
Print Space(18); a
a = Mid("awesome", 4, 1)
Print Space(20); a

align right :
a = Mid("awesome", 1, 7)
Print Space(10); a
a = Mid("awesome", 2, 6)
Print Space(12); a
a = Mid("awesome", 3, 5)
Print Space(14); a
a = Mid("awesome", 4, 4)
Print Space(16); a
a = Mid("awesome", 5, 3)
Print Space(18); a
a = Mid("awesome", 6, 2)
Print Space(20); a
a = Mid("awesome", 7, 1)
Print Space(22); a```

I want to make it in a loop, but i cant figure out how to.
anyone have ideas?

#2
January 15th, 2013, 01:35 PM
 medialint
Type Cast Exception

Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,948
Time spent in forums: 6 Months 2 Weeks 2 Days 5 h 43 m 32 sec
Reputation Power: 8520
I'd scrap the example code on the assignment and do it without all the unnecessary nonsense myself.

Just make one single "awesome" string and iterate through it

Code:
```strVar = "awesome"
For n=1 to len(strVar)
a = mid(strVar, n, 1)
' etc
Next```
ragefour agrees!
__________________
medialint.com

“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss

Last edited by medialint : January 15th, 2013 at 01:37 PM.

#3
January 15th, 2013, 04:03 PM
 couttsj
Contributing User

Join Date: Aug 2011
Posts: 270
Time spent in forums: 4 Days 1 h 17 m 9 sec
Reputation Power: 44
Code:
```Option Explicit
Const s As String = "awesome"

Private Function AlignRight(iPtr As Integer) As String
AlignRight = Space(8 + 2 * iPtr) & Mid(s, iPtr, 8 - iPtr)
End Function

Private Function Diamond(iPtr As Integer) As String
Diamond = Space((iPtr + 6) * 2) & Mid(s, iPtr, 9 - 2 * iPtr)
End Function

Private Sub cmdAlignR_Click()
Dim N%
For N% = 1 To 7
Print AlignRight(N%)
Next N%
End Sub

Private Sub cmdDiamond_Click()
Dim N%
For N% = 4 To 1 Step -1
Print Diamond(N%)
Next N%
For N% = 2 To 4
Print Diamond(N%)
Next N%
End Sub```

J.A. Coutts
ragefour agrees!

#4
January 16th, 2013, 07:23 AM
 ragefour
Registered User

Join Date: Jan 2013
Posts: 2
Time spent in forums: 19 m 44 sec
Reputation Power: 0
Thanks guy's for this.
I really appreciate it.

 Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Diamond and align right using string manipulation help