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

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




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




PostPosted: Wed Feb 26, 2003 2:40 am   Post subject: (No 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"
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
m4rk




PostPosted: Thu Feb 27, 2003 10:51 pm   Post subject: (No subject)

thanks alot, i was stuck on this problem for about a week
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  [ 3 Posts ]
Jump to:   


Style:  
Search: