Posted: 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...
Turing:
var sent, pattern, templet :string var a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, pos, count, size, lensent, long :int:=0 %This program takes a sentence and tells you the total characters, longest word, total words in the sentence, and %total number of each letter. loop
put"Hello, please enter a sentence and hit enter when done."..
put"End program with 'end'." get sent :*
if sent ="end"or sent ="End"or sent ="'end'"or sent ="END"then exit else endif
for I :1.. lensent
templet := sent (I) %Individual letter count put templet
if templet ="a"or templet ="A"then
a := a + 1 elsif templet ="b"or templet ="B"then
b := b + 1 elsif templet ="c"or templet ="C"then
c := c + 1 elsif templet ="d"or templet ="D"then
d := d + 1 elsif templet ="e"or templet ="E"then
e := e + 1 elsif templet ="f"or templet ="F"then
f := f + 1 elsif templet ="g"or templet ="G"then
g := g + 1 elsif templet ="h"or templet ="H"then
h := h + 1 elsif templet ="i"or templet ="I"then
i := i + 1 elsif templet ="j"or templet ="J"then
j := j + 1 elsif templet ="k"or templet ="K"then
k := k + 1 elsif templet ="l"or templet ="L"then
l := l + 1 elsif templet ="m"or templet ="M"then
m := m + 1 elsif templet ="n"or templet ="N"then
n := n + 1 elsif templet ="o"or templet ="O"then
o := o + 1 elsif templet ="p"or templet ="P"then
p := p + 1 elsif templet ="q"or templet ="Q"then
q := q + 1 elsif templet ="r"or templet ="R"then
r := r + 1 elsif templet ="s"or templet ="S"then
s := s + 1 elsif templet ="t"or templet ="T"then
t := t + 1 elsif templet ="u"or templet ="U"then
u := u + 1 elsif templet ="v"or templet ="V"then
v := v + 1 elsif templet ="w"or templet ="W"then
w := w + 1 elsif templet ="x"or templet ="X"then
x := x + 1 elsif templet ="y"or templet ="Y"then
y := y + 1 elsif templet ="z"or templet ="Z"then
z := z + 1 elsif templet =" "then
count := count + 1
endif endfor %Number of each letter and word output
put"Total A's: ", a
put"Total B's: ", b
put"Total C's: ", c
put"Total D's: ", d
put"Total E's: ", e
put"Total F's: ", f
put"Total G's: ", g
put"Total H's: ", h
put"Total I's: ", i
put"Total J's: ", j
put"Total K's: ", k
put"Total L's: ", l
put"Total M's: ", m
put"Total N's: ", n
put"Total O's: ", o
put"Total P's: ", p
put"Total Q's: ", q
put"Total R's: ", r
put"Total S's: ", s
put"Total T's: ", t
put"Total U's: ", u
put"Total V's: ", v
put"Total W's: ", w
put"Total X's: ", x
put"Total Y's: ", y
put"Total Z's: ", z
put"Total words: ", count + 1
endloop
put"Thank you!"
Mod Edit: Remember to use syntax tags! Thanks
code:
[syntax="turing"]Code Here[/syntax]
Sponsor Sponsor
DanielG
Posted: 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.
L337User
Posted: 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
DanielG
Posted: 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.
L337User
Posted: 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
A.J
Posted: 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...
L337User
Posted: 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
DanielG
Posted: 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 := "".
Sponsor Sponsor
L337User
Posted: 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
DanielG
Posted: 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.
L337User
Posted: 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
DanielG
Posted: 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.
L337User
Posted: 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
Turing:
if templet =" "then
number = templet
if number > templet then
longword = number
else
number = templet
endif else endif
L337User
Posted: 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
DanielG
Posted: Sun Jan 18, 2009 4:38 pm Post subject: RE:Longest word and how long program
first, you wrote
code:
number = templet
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.