Computer Science Canada

array -> string

Author:  Selfi [ Fri Jun 11, 2004 12:04 pm ]
Post subject:  array -> 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.

code:

% 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

Author:  aside [ Fri Jun 11, 2004 12:12 pm ]
Post subject: 

i assume that you are trying to put each indivisual letter in an array back into a whole word?
code:

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.

Author:  Selfi [ Fri Jun 11, 2004 12:25 pm ]
Post subject: 

thats the problem see, is that you need to declare the newword first to add to it, which is my dilemma

Author:  Selfi [ Fri Jun 11, 2004 12:26 pm ]
Post subject: 

and your way prints them back in reverse lol

Author:  Selfi [ Fri Jun 11, 2004 12:28 pm ]
Post subject: 

this'll do it Very Happy
code:
% 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

Author:  naoki [ Fri Jun 25, 2004 4:18 pm ]
Post subject: 

of course, for efficiency reasons, you could put both in the same loop
and assign newword as

newword += word (i)
letter (i) := word (i)


: