Printing string elements like array

New to FreeBASIC? Post your questions here.
Kouga
Posts: 2
Joined: Nov 30, 2018 4:14

Printing string elements like array

Postby Kouga » Dec 02, 2018 1:07

Alright, I am having a hard time figuring this one out. I'm switching from c++ to freebasic since it is better documented and I'm having a hard time figuring out how to print out individual letters from a string as array elements.

Something like:
dim a as string
a = "Hello there"
print a(2)

I know it errors, but I can't quite find how to do it the correct way. I'm wanting to make a game that has a "person" typing back to you and this would help out a lot
counting_pine
Site Admin
Posts: 6026
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Printing string elements like array

Postby counting_pine » Dec 02, 2018 16:23

Hi Kouga, welcome to the forum. :)
You're close, except string indexing uses '[]' like in C++, and it also returns the ASCII character code rather than the letter itself.
So you could use chr() to convert the code to a string:

Code: Select all

print chr(a[2]) '' "l"

Or you could use the mid() function to take a substring of length 1:

Code: Select all

print mid(a, 3, 1) '' "l"

Note that mid() starts at 1, while string indexing starts at 0.

mid() is safer because it won't read memory outside of the bounds of the string, but is probably a little less efficient.
dodicat
Posts: 5332
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Printing string elements like array

Postby dodicat » Dec 02, 2018 16:31

a(2) would be an element of a freebasic string array.
However you can use string indexing, which would be more like a C array, except that each element holds the ascii value of a character.

Code: Select all

 
dim a as string
a = "Hello there"
print a[2]
print chr(a[2])
print chr(a[0])

'for array (zero based)

dim as long length=len(a)

dim as string b(0 to length-1)
for n as long=0 to length-1
    b(n)=chr(a[n])
next n
print
print b(2)
sleep
dodicat
Posts: 5332
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Printing string elements like array

Postby dodicat » Dec 02, 2018 16:32

Oops!, I didn't see your post counting pine.
Munair
Posts: 802
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: Printing string elements like array

Postby Munair » Dec 02, 2018 23:16

If speed is a thing you can also get the character like: c = strptr(a)[2]
Munair
Posts: 802
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: Printing string elements like array

Postby Munair » Dec 02, 2018 23:25

See also this thread (I just happened to work on this thingy):
viewtopic.php?f=7&t=27203&p=255223#p255223
Kouga
Posts: 2
Joined: Nov 30, 2018 4:14

Re: Printing string elements like array

Postby Kouga » Dec 05, 2018 4:05

Thank you everyone for the response, and the welcome :). Thanks for the advice counting_pine and dodicat, and gonna read over that Munair!


EDIT:

Just wanted to add what I turned the suggestions into :), thanks again everyone

Code: Select all

dim a as string
a = "Why hello there"
dim b as integer
dim c as integer
dim d as integer
b = len(a)
print b
sleep
c = 0
d = 0
while c<b+1
    print chr(a[d]);
    c=c+1
    d=d+1
    sleep
wend
sleep

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest