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

Username:   Password: 
 RegisterRegister   
 An array that stores each word of a sentence as its own var.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cohen_ohyea




PostPosted: Sun Jan 11, 2004 3:59 pm   Post subject: An array that stores each word of a sentence as its own var.

Can some one help me with code that gets a sentence from the user and stores each words as its own variable?? The code has to work with a sentence 1 word long or 100 words long depending on what the user inputs as there sentence. Ur help is much appreciated. Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Jan 11, 2004 5:19 pm   Post subject: (No subject)

use substrings and index() to break the sentance up into words, then store them in a dynamic array.

code:

var sentance:string
var num:int
put "enter sentance"
get sentance

num:= index(" ", sentance) %index arguments might be in wrong order
put sentance(1..num)


this should isolate the first word of the sentance... Thinking I think
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
McKenzie




PostPosted: Sun Jan 11, 2004 5:28 pm   Post subject: (No subject)

code:
var sent:string
var spot:int
var words:array 1.. 1000 of string
var numWord :int
put "What you got?"

get sent:*
sent := sent + " "
loop
    spot:= index(sent," ")
    exit when spot = 1
    words(numWord) := sent(1..spot - 1)
    numWord := numWord +1
    sent := sent(spot+1..*)
end loop


Sorry, I'd prefer to explain the problem, but I gotta go
cohen_ohyea




PostPosted: Sun Jan 11, 2004 8:10 pm   Post subject: (No subject)

thank you guys for your help but i dont think that is gonna work. The program I am working on has to get a sentence from the user and then the user has to be able to search for a word in the sentence they inputted and the program should display how many times the word they searched for appears. When i use the index command with your program it doesnt quite work out. any ideas.
Andy




PostPosted: Sun Jan 11, 2004 8:38 pm   Post subject: (No subject)

wow why didnt u tell us earlier?

code:

var sentence, word : string
put "Enter Sentence"
get sentence : *
put "Enter Word"
get word : *
var count := 0
loop
    exit when index (sentence, word) = 0
    count += 1
    sentence := sentence (index (sentence, word) + length (word) .. *)
end loop
put "the word ", word, " appeared ", count, " times"

cohen_ohyea




PostPosted: Sun Jan 11, 2004 8:45 pm   Post subject: (No subject)

dodge_tomahawk you are the best!!! Very Happy Very Happy Very Happy thanks soo much ive been working really hard on my program for school but so far it does nothing but looks pretty. now i can finish it Very Happy
Andy




PostPosted: Sun Jan 11, 2004 8:54 pm   Post subject: (No subject)

u noe when u ask for help, you're supposed to tell us what u want to achieve, what ur thinking of doing, then some code to prove to us that you actually tried something so we can help more efficiently.. u dont want a whatdotcolor method for that do u?
CITC




PostPosted: Sun Jan 11, 2004 9:01 pm   Post subject: (No subject)

jeeze I would want a whatdotcolour method for that!! that would be so cool!!
Andy




PostPosted: Sun Jan 11, 2004 9:05 pm   Post subject: (No subject)

will start tomorrow
cohen_ohyea




PostPosted: Sun Jan 11, 2004 9:10 pm   Post subject: (No subject)

sorry. how would i alter your code so that it isnt case sensitive, checks "Word" along with "word" or "WORD. Confused
McKenzie




PostPosted: Sun Jan 11, 2004 9:43 pm   Post subject: (No subject)

Dodge,
the obvious answer is to load a 2D array with the colours of the word you are looking for then scan across the sentence one character at a time (it's a monospaced font so it's not to bad) You do have to consider multiline answers however.
If you're going to use whatdotcolour, why be obvious? The cadillac solution is the to take a picture of the word then use picXor (you all read my picXor tut right???) With the picXor if the word matched all pixels would be white, if it didn't you could undo it buy XORing it again Very Happy

As far as upper/lower problems you have 2 answers
1. Use C, it strupr works great
2. make your own toUpper function.
code:
1. make an empty "" string variable to build up your answer
2. check letter by letter through the word (for loop)
if it is lower case add on it's uppercase version (it was an ascii value 32 lower like ans := ans + chr(ord(word(i)+32)))
3. result the answer
4. When you call the index function pass both the word and the sentence to your toupper function like:
index(toupper(sentence),toupper(word))
Andy




PostPosted: Mon Jan 12, 2004 11:53 am   Post subject: (No subject)

well i got the whatdotcolor solution...

code:

var sentence, word : string
put "Enter Sentence"
get sentence : *
put "Enter Word"
get word
fcn whatdotcolorWordsearch (sentence, word : string) : int
    View.Set ("offscreenonly")
    var picID, count : int
    count := 0
    picID := Pic.New (1, 1, length (sentence), 1)
    for i : 1 .. length (sentence)
        drawdot (i, 1, ord (sentence (i)))
    end for

    for i : 1 .. length (sentence) - length (word) + 1
        for j : 1 .. length (word)
            if ord (word (j)) not= whatdotcolor (i + j - 1, 1) then
                exit
            elsif j = length (word) then
                count += 1
            end if
        end for
    end for
    Pic.Draw (picID, 1, 1, picCopy)
    View.Set ("nooffscreenonly")
    result count

end whatdotcolorWordsearch

put whatdotcolorWordsearch (sentence, word)
McKenzie




PostPosted: Mon Jan 12, 2004 3:20 pm   Post subject: (No subject)

Good job Dodge, you have proven that whatdotcolour is the best function ever Laughing
Andy




PostPosted: Mon Jan 12, 2004 8:07 pm   Post subject: (No subject)

exactly.. so all u kids who think u're all that with ur string manipulation... forget everything u've ever learned and change to whatdotcolor...

p.s. i will offer classes on whatdotcolor for 5 bux donation/2 hr to massey robotics team
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  [ 14 Posts ]
Jump to:   


Style:  
Search: