Skipping last word
Author |
Message |
cat8864
|
Posted: Tue Nov 25, 2008 2:10 pm Post subject: Skipping last word |
|
|
The goal os to create a program that finds the largest word in the sentence and displays it. I've managed that but it always skips the last word in the sentence even if it's the longest. Any ideas?
Turing: |
var sentence := ""
var spot := 0
var longest := ""
var removed := ""
var x := 0
var tally := 0
put "Please enter a sentence of two or more words"
get sentence : *
put " "
var sentence1 := sentence
loop
exit when sentence1 = ""
% Selects 1st word in current line
loop
put spot
spot := spot + 1
if length (sentence1 ) = spot then
% removed := sentence1
x := x + 1
elsif sentence1 (spot ) = " " then %selects characters up to first space
removed := sentence1 (1 .. spot )
x := x + 1
end if
exit when x = 1
end loop
% Removes 1st word
% Stores word for future comparison
sentence1 := sentence1 (spot + 1 .. *) %creates new sentence without 1st word
if length (removed ) > length (longest ) then
longest := removed
elsif length (removed ) = length (longest ) then
tally := tally + 1
end if
% Removes the spaces before the next word
loop
x := 0
if sentence1 = "" then
x := x + 1
elsif sentence1 (1) = " " then
sentence1 := sentence1 (2 .. *)
end if
exit when x = 1
exit when sentence1 (1) not= " "
end loop
% Reset values
spot := 0
x := 0
put removed
put sentence1
put " "
end loop
put "The removed word is ", longest
put "You have ", tally - 1, " words the same length"
put " "
put "The remaining sentence is ", sentence1
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
MihaiG
![](http://compsci.ca/v3/uploads/user_avatars/2070081168483773c3dead4.png)
|
Posted: Tue Nov 25, 2008 9:08 pm Post subject: Re: Skipping last word |
|
|
hey cat8864
code: |
var st : string
get st : *
st := st + " "
var pos : int := 1
var temp1, temp2 : string := ""
for i : 1 .. length (st)
pos := index (st, " ")
exit when pos = 0
temp2 := st (1 .. (pos - 1))
if length (temp2) = length (temp1) then
if temp2 > temp1 then
temp1 := st (1 .. (pos - 1))
end if
else
if length (temp2) > length (temp1) then
temp1 := st (1 .. (pos - 1))
end if
end if
st := st (pos + 1 .. *)
end for
put "-----------------------"
put "The removed word is ", temp1
|
i just wrote that up, what i did is i would check for spaces using the index function and then get store the word from the beginning to that point in a temporary varaible, i would then delete the first part of the sentance and repeat the process, if i got two words that had the same length i compared them arithmetically (so which was is further in the dictionary type of thing), hopefully this should help somehow, i also added a space at the end of the program so when i tried to index that last word it would find the end, rember if the index cannot find the required phrase in the string it will return a value of 0, so i am assuming that will never happen till it hits the end, so if that does happen that means im done parsing the string
i wont comment the code, so you can take a look at it and try to understand how it works, if you have any questions feel free to post them, note, i did not do the tallying, you already had that implmented in your code |
|
|
|
|
![](images/spacer.gif) |
cat8864
|
Posted: Tue Nov 25, 2008 10:28 pm Post subject: Re: Skipping last word |
|
|
Lets see if I've got this right:
1. the (pos - 1) finds the position of then end of the first word.
2. temp2 is the first word of the string up to but not including the space
3. temp1 is an empty string which becomes temp2 (stores the word if larger than the previous)
4. the sentence becomes the sentence - temp2
5. repeat until the position of the " " = 0 or the only thing left in the string is " "
what I don't get is the first part of the if statement part:
if length (temp2) = length (temp1) then
if temp2 > temp1 then
temp1 := sentence (1 .. (pos - 1))
end if
And I still don't know what I did wrong in my code though |
|
|
|
|
![](images/spacer.gif) |
MihaiG
![](http://compsci.ca/v3/uploads/user_avatars/2070081168483773c3dead4.png)
|
Posted: Wed Nov 26, 2008 1:33 am Post subject: Re: Skipping last word |
|
|
1. yes
2. yes
3. yes
4. not really that way, but i concactenate the first part of the string inluding the space,
5. yes
what that snippet of code did is
if you have two words such as seven and hello, you can see they are the same length, but seven is "larger" cause the letters it uses are further up in the alphabet,
just try re-writing your code, that usually can helps cause you might take a different approach |
|
|
|
|
![](images/spacer.gif) |
cat8864
|
Posted: Wed Nov 26, 2008 10:52 pm Post subject: RE:Skipping last word |
|
|
Thanks for the help! |
|
|
|
|
![](images/spacer.gif) |
|
|