
-----------------------------------
Selfi
Fri Jun 11, 2004 12:04 pm

array -&gt; string
-----------------------------------
im trying to turn an array into a string, heres what i have so far, i just dont know how to put it into a string. in the program im coding i get the word from a file, which is why i have a get there. this is NOT the program that i am coding, i just need some help with an example, thanks.


% this program inputs individual letters from a word into an array, then
% puts them back into a string
var word, neW : string

get word % gets the word 

var letter : array 1 ..length(word) of string %variable to store letters

for i : 1 .. length (word)
    letter (i) := word (i) %assigning each value to the letter position
end for

for i : 1 .. length (word)
    put letter (i) .. %displaying on screen
end for

put neW % displays our recreated string

-----------------------------------
aside
Fri Jun 11, 2004 12:12 pm


-----------------------------------
i assume that you are trying to put each indivisual letter in an array back into a whole word?

var word, newword:string
get word
var letter:array 1..length (word) of string
for i:1..length (word)
letter (i):=word(i)
end for
for decreasing i:length (word)..1
newword+=newword+letter(i)
end for
put newword

this program will put word into an array and reverse the order they are in when they are put into newword. although you can do it another way.

-----------------------------------
Selfi
Fri Jun 11, 2004 12:25 pm


-----------------------------------
thats the problem see, is that you need to declare the newword first to add to it, which is my dilemma

-----------------------------------
Selfi
Fri Jun 11, 2004 12:26 pm


-----------------------------------
and your way prints them back in reverse lol

-----------------------------------
Selfi
Fri Jun 11, 2004 12:28 pm


-----------------------------------
this'll do it :D
% this program inputs individual letters from a word into an array, then 
% puts them back into a string 
var word, newword : string :=""

get word % gets the word 

var letter : array 1 ..length(word) of string %variable to store letters 

for i : 1 .. length (word) 
    letter (i) := word (i) %assigning each value to the letter position 
end for 
put " "
for i : 1 .. length (word) 
    newword:=newword+letter(i)
end for 

put newword % displays our recreated string


-----------------------------------
naoki
Fri Jun 25, 2004 4:18 pm


-----------------------------------
of course, for efficiency reasons, you could put both in the same loop
and assign newword as

newword += word (i)
letter (i) := word (i)
