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

Username:   Password: 
 RegisterRegister   
 A quick question about strings . . .
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nyrd




PostPosted: Thu Feb 14, 2008 8:30 pm   Post subject: A quick question about strings . . .

I was told by my teacher that strings are essentially arrays of string(1).
This makes sense, because we can compare each individual character and such right?

code:

if string1(1) < string 2(1) then
. . .


The problem is that when I try assigning each individual character a new value,

code:

var it : string := "bob"
it(1) := a


it pops up with an error saying that "the left side of the assignment is not a variable and hence cannot be assigned to "
why does this happen?
Sponsor
Sponsor
Sponsor
sponsor
Mackie




PostPosted: Thu Feb 14, 2008 8:55 pm   Post subject: RE:A quick question about strings . . .

It is an array but, not exactly like the array's you make. They can only be used for reference, not assignment.


Try something like this.

code:
var word : string := "bob"
word := "a" + word (2..3)


It's a pain.
Nyrd




PostPosted: Thu Feb 14, 2008 9:13 pm   Post subject: Re: A quick question about strings . . .

Hmmm . . . Pain indeed

Is there any relatively efficient way to make that work such that I could replace any character, or am I just better off creating an actual array?
Mackie




PostPosted: Thu Feb 14, 2008 9:21 pm   Post subject: RE:A quick question about strings . . .

Ah well it depends what you want to use it for.
Nyrd




PostPosted: Thu Feb 14, 2008 9:24 pm   Post subject: Re: A quick question about strings . . .

Never mind, after a bit of tinkering that way can be made to work pretty well.
Here's what I came up with if anyone is interested Very Happy

Turing:


var word : string := "oooo"
var replacement :string := "x"
var num :=1  %Where the replacement character is to go

word :=  word (1..num-1) +replacement + word (num+1..length(word))
put word

Nyrd




PostPosted: Thu Feb 14, 2008 9:25 pm   Post subject: RE:A quick question about strings . . .

I was actually meaning to use it for a code breaking program. What I came up with above seems to fit my purposes though. Thanks anyway!
Mackie




PostPosted: Thu Feb 14, 2008 9:28 pm   Post subject: RE:A quick question about strings . . .

No problem. It's good that you took the time to figure it out on your own. We don't see a lot of that around here.
Nyrd




PostPosted: Thu Feb 14, 2008 9:39 pm   Post subject: RE:A quick question about strings . . .

Anything to help you help me!
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Feb 15, 2008 1:12 am   Post subject: Re: A quick question about strings . . .

Nyrd @ Thu Feb 14, 2008 9:24 pm wrote:
... + word (num+1..length(word))

I'm not sure if this works, but I think in Turing you can also do things like
Turing:

word(num .. *)

and even
Turing:

word(num .. * - 1)

It's just syntactical sugar though.

Also -- does your code work when word := "o" ? (I don't have Turing accessible at the moment)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ericfourfour




PostPosted: Fri Feb 15, 2008 10:58 am   Post subject: RE:A quick question about strings . . .

When dealing with strings the * operator means end (or length of the string). It is read as an integer so you can do arithmetic with it. What Tony suggested will work.

If num exceeds the length of word, it will crash. You should put in an if statement that verifies num is in range.
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  [ 10 Posts ]
Jump to:   


Style:  
Search: