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

Username:   Password: 
 RegisterRegister   
 skipping white spaces help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Jas




PostPosted: Wed May 18, 2005 9:11 pm   Post subject: skipping white spaces help

what if an array record had a value with white spaces at the end, and u wanted to save that value, into a different variable, but without the white spaces, how would u do that. This is an example of what i mean



code:


% WithWhiteSpace(1).name can have value of "Bob          "

for i : 1..count
WithoutWhiteSpace(i).name := WithWhiteSpace(i).name, skip
end for



when i use the ", skip" i get the error "Syntax error at ',' "
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Wed May 18, 2005 9:31 pm   Post subject: (No subject)

Well, if you can assume that the *only* white space that exists is at the end of the string, the index() the position of the first space, and extract the required string.
If you have more white space, some nifty index()ing will solve things for you.

If you don't know any of this, look up String Manipulation in the Tutorials. Or check out Cervantes list of Tutorials...it will help a lot!
Jas




PostPosted: Wed May 18, 2005 9:55 pm   Post subject: (No subject)

acutally, the variable could have a first and last name. but the variable for sure has spaces at the end. for example:

code:


%WithWhiteSpace(1).name can have value of "Bob Grey Smith          "





How would you ignore the spaces in between, but "delete" the spaces at the end?
Jas




PostPosted: Thu May 19, 2005 12:28 am   Post subject: (No subject)

ok took me quite a while to figure out, but i ended up with this:

code:

var word1 : string
var word : string
put "Enter full name (with spaces at the end): " % eg: "Bob Grey      "
get word : * % max 3 words

if index (word, " ") not= 0 then
    var place : int
    place := index (word, " ")
    if index (word (place .. place + 1), " ") not= 0 and index (word (place + 1 .. place + 2), " ") not= 0 then
        word1 := word (1 .. place - 1)
    elsif index (word (place + 1 .. *), " ") not= 0 then
        place := index (word (place + 1 .. *), " ") + place
        if index (word (place + 1 .. *), " ") not= 0 and index (word (place + 1 .. place + 2), " ") not= 0 then
            word1 := word (1 .. place - 1)
        elsif index (word (place + 1 .. *), " ") not= 0 then
            place := index (word (place + 1 .. *), " ") + place
            if index (word (place + 1 .. *), " ") not= 0 and index (word (place + 1 .. place + 2), " ") not= 0 then
                word1 := word (1 .. place - 1)
            end if
        end if
    end if
end if

put word1, "abc" % no spaces at end of word!!! :)
Bacchus




PostPosted: Thu May 19, 2005 6:06 am   Post subject: (No subject)

couldnt you just do:
code:
var word:string
get word:*
word:=word(1..index(word,"  ")-1)
put word
MysticVegeta




PostPosted: Thu May 19, 2005 6:23 am   Post subject: (No subject)

Yes but what if the spaces are : "Bob is bad "
None of them works for that so here is my code:
code:
var a : string
get a : *
var count : int
loop
    for s : 1 .. length (a)
        if a (s) = " " then
            count := s
            exit
        end if
    end for
    if ((count + 1) > length (a)) or (count = length (a)) then
        a := a (1 .. count - 1)
    else
        a := a (1 .. count - 1) + a (count + 1 .. *)
    end if
    if index (a, " ") = 0 then
        put a
        exit
    end if
end loop
Cervantes




PostPosted: Thu May 19, 2005 7:06 am   Post subject: (No subject)

I'm pretty sure he doesn't want all the spaces deleted, only the ones at the end:
Turing:

var name : string
get name : *
for decreasing i : length (name) .. 1
    if name (i) ~= " " then
        name := name (1 .. i)
        exit
    end if
end for
put name ..
put "."

The put "." is simply to show where the name string ends.
Jas




PostPosted: Thu May 19, 2005 4:18 pm   Post subject: (No subject)

wow, u really shortened it up, im impressed Cervantes!!
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: