Computer Science Canada Longest word and how long program |
Author: | L337User [ Sun Jan 18, 2009 12:41 pm ] | ||||
Post subject: | Longest word and how long program | ||||
Okay, so I'm a grade 10 student in B.C. and I've had to make a program that says how many words there are in a sentence, how many letters, and what letters are in it. My problem is I need to add a part that calculates the longest word and tells how long it is I have no idea how to do this and I have been trying to do this for a very long time now. I'm very bad at rogramming and need some assistance as to what I should do in order to complete this. Below is the program so far...
Mod Edit: Remember to use syntax tags! Thanks
|
Author: | DanielG [ Sun Jan 18, 2009 1:17 pm ] |
Post subject: | RE:Longest word and how long program |
you can make your life so much easier by using ord and chr. for longest word you can coutt how many words there are between spaces/end of string. |
Author: | L337User [ Sun Jan 18, 2009 1:20 pm ] |
Post subject: | Re: Longest word and how long program |
Could you please give me a sample program because seriously im really bad at this I have no idea what ord or chr is so could you please give me an example im personally not enjoying programming and jsut need to finish |
Author: | DanielG [ Sun Jan 18, 2009 1:24 pm ] |
Post subject: | RE:Longest word and how long program |
wait, do you know array, ord and chr don't help if you don't know arrays. |
Author: | L337User [ Sun Jan 18, 2009 1:36 pm ] |
Post subject: | RE:Longest word and how long program |
I have know idea what array is this was stricly a string processing assignment |
Author: | A.J [ Sun Jan 18, 2009 1:49 pm ] |
Post subject: | Re: Longest word and how long program |
well, I am assuming your teachers arent going to mess you up with HUGE sentences (that go over 256 characters) here is a small example that might help you understand what DanielG is trying to say: "Hi, my name is Hitler." The above sentence has 4 spaces, and therefore it has 5 words (the number of words is usually number of spaces + 1) For the longest word however, what you can do is keep storing the next character of the sentence (if it is a letter) into a string, until you reach a space (this means that the string in which you stored the letters now contain a whole word). Check to see if it is bigger than the word you had before. If it is, then replace it with the word you just got, empty the string, and continue with your search. Using this algorithm on the above sentence, you would get : "i, my name is Hitler." string = "H" next character -> i ", my name is Hitler." string = "Hi" next character -> , ", my name is Hitler." string = "Hi" (the comma isn't added to the string as it isn't a letter) next character -> Since the next character is a space, we now check the word we have in the stored string. It is "Hi", which has a length of 2. Store it as you longest word (since it is the first word you got). Empty the string. "my name is Hitler." string = "" next character -> m "y name is Hitler." string = "m" next character -> y " name is Hitler." string = "my" next character -> Since "my" isn't longer than "Hi", we just empty the string and continue Continuing this, we eventually get "Hitler" as the longest word. The one small problem that you have to face is that at the end of most sentences, there aren't any spaces, so this algorithm tends to ignore the last word. So add an extra step to check if you have reached the end of the sentence. I hope this helped... |
Author: | L337User [ Sun Jan 18, 2009 2:12 pm ] |
Post subject: | RE:Longest word and how long program |
Thank you it did that is sort of what I was trying to go for but i really need help with the actually structure and keywods I need to use because i cant seem to manipulate what i know in order to accomplish this |
Author: | DanielG [ Sun Jan 18, 2009 2:31 pm ] |
Post subject: | RE:Longest word and how long program |
all you have to do is check is Sent(I) = ' ', if it is, you finished a word. for word, you write longest := longest + sent(I) when resetting write longest := "". |
Author: | L337User [ Sun Jan 18, 2009 3:29 pm ] |
Post subject: | RE:Longest word and how long program |
yeah i dont know how to do any of that can any of you simply add the part to my program mentioned in the first post so that i can understnad what im looking for |
Author: | DanielG [ Sun Jan 18, 2009 3:42 pm ] |
Post subject: | RE:Longest word and how long program |
we can't do you projects for you, try writing it yourself and we could help you understand where you went wrong. |
Author: | L337User [ Sun Jan 18, 2009 4:16 pm ] |
Post subject: | RE:Longest word and how long program |
how about something like... if templet = " " then number = templet if number > templet then longword = number else number = templet else end if |
Author: | DanielG [ Sun Jan 18, 2009 4:25 pm ] |
Post subject: | RE:Longest word and how long program |
what is number? (the variable) I don't exactly get what you are trying to do with your code (even though it is supposed to be what others here (as well as myself) adviced). at least use turing code syntax, it will make it easier to read. |
Author: | L337User [ Sun Jan 18, 2009 4:30 pm ] | ||
Post subject: | RE:Longest word and how long program | ||
im sorry i dotn know how hopefully this works
|
Author: | L337User [ Sun Jan 18, 2009 4:33 pm ] |
Post subject: | RE:Longest word and how long program |
i know my code is really weird but i really dont understand programming ive been trying but now i just cant complete this area and i have an hour left to do this |
Author: | DanielG [ Sun Jan 18, 2009 4:38 pm ] | ||
Post subject: | RE:Longest word and how long program | ||
first, you wrote
I don't get what you trying to do there, since this is not an if statement and if you are trying to assign the value then you made a mistake with the number > templet statement. What you need to do is constantly add templet to your longword, when you reach the end of the string or " ", you check if the length of longword (using length(longword)) is greater then best length (make another variable), if it is, make your longword answer the longword and reset longword. Hope you got what I was trying to say. |
Author: | L337User [ Sun Jan 18, 2009 4:44 pm ] |
Post subject: | RE:Longest word and how long program |
thank you that actually works a lot better i'll post a sample of waht i think in a sec |
Author: | L337User [ Sun Jan 18, 2009 4:49 pm ] | ||
Post subject: | RE:Longest word and how long program | ||
|
Author: | DanielG [ Sun Jan 18, 2009 4:57 pm ] |
Post subject: | RE:Longest word and how long program |
first, there is no point for bestlength = bestlength, it is redundant. you also need something to tell it to reset when you have the ' ' character. other than that and the fact that you need to rearrange it, it seems ok ALso, the length command is length(), not leng(). |
Author: | A.J [ Sun Jan 18, 2009 6:13 pm ] |
Post subject: | Re: Longest word and how long program |
try writing the whole program... and the 3rd DWITE's # 2 is essentially what you are trying to do (sort of) try looking up my (or Daniel's) team's code (as we used turing) we are O(512) and O(n) |