Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 array -> string
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Selfi




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
aside




PostPosted: Fri Jun 11, 2004 12:12 pm   Post subject: (No 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.
Selfi




PostPosted: Fri Jun 11, 2004 12:25 pm   Post subject: (No subject)

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




PostPosted: Fri Jun 11, 2004 12:26 pm   Post subject: (No subject)

and your way prints them back in reverse lol
Selfi




PostPosted: Fri Jun 11, 2004 12:28 pm   Post subject: (No 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
naoki




PostPosted: Fri Jun 25, 2004 4:18 pm   Post subject: (No 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)
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: