Computer Science Canada

help with string manpulation

Author:  m4rk [ Tue Feb 25, 2003 11:29 pm ]
Post subject:  help with string manpulation

can i write a program that inputs a sentence and two characters, the second character should replace the first character in the sentence
i.e Sentence="Computer nerds make millions"
oldchar = "m"
newchar="n"
Output should be: Conputer nerds nake nillions

Author:  Tony [ Wed Feb 26, 2003 2:40 am ]
Post subject: 

i think that index() returns the position at which it finds the substring, but that wouldn't do us too much good.

so you use stringvariable(index0 to access substrings. Few example to make you understand.

var name:string := "swat"

name(1) returns "s"
name(2) returns "w"
name(1..2) returns "sw"
name(3..*) returns "at"
name(1+1 .. *-1) returns "wa"

the first value is the start of substring, 2nd is end. Substring is generated between the two values (including them). You can use * instead of using length() function, it does the same thing. And ofcourse you can use math operations and use variables as values for max control.

so

code:

var text:string := "aaabbbcccaaa"
var textnew:string := ""

for i:1..length(text)
if text(i) = "a" then
textnew += "x"
else
textnew += text(i)
end if
end for


This code reads the string 1 letter at a time and if it finds a letter to replace, it does so. In this case it replaces "a" with "x"

Author:  m4rk [ Thu Feb 27, 2003 10:51 pm ]
Post subject: 

thanks alot, i was stuck on this problem for about a week


: