Author |
Message |
Houdini
|
Posted: Thu Apr 02, 2009 6:37 pm Post subject: How to count 2 lettered words in sentence |
|
|
I'm creating a program that allows you to type in a sentence and the output will be the total amount of words and how many 2-lettered words there are.. I'm not sure how to do the second part... I tried alot using two counters in my for loop but didn't work.. Here is the code I have so far:
Turing: |
var sentence : string
var words : int := 1
put "Please type in a sentence and hit enter"
get sentence : *
for x : 1 .. length (sentence )
if sentence (x ) = " " then
words := words + 1
end if
end for
put "There is a total of ", words, " words"
|
Mod Edit: Fixed the syntax tags code: | [syntax="turing"]Code Here[/syntax] |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
tjmoore1993
|
Posted: Thu Apr 02, 2009 7:05 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
I'm not sure if this will help but why not check out string manipulation tutorials? |
|
|
|
|
|
saltpro15
|
Posted: Thu Apr 02, 2009 7:06 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
maybe check the length of the word? for example
Turing: |
for i : 1.. length(word )
counter += 1
end for
if counter > 1 and counter < 3 then
word + = 1
end for
|
does that work? |
|
|
|
|
|
Houdini
|
Posted: Thu Apr 02, 2009 7:16 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
No it doesn't work. The output is 1 more word than what it should be. |
|
|
|
|
|
chrisbrown
|
Posted: Thu Apr 02, 2009 7:20 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
Turing: | if counter >1 and counter <3 then |
Seriously?? I hope that was just a brain fart. |
|
|
|
|
|
CodeMonkey2000
|
Posted: Thu Apr 02, 2009 7:20 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
Well why don't you count how many letters it took to get to a space? If it is 2 then it is a 2 lettered word. |
|
|
|
|
|
Houdini
|
Posted: Thu Apr 02, 2009 7:25 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
@ CodeMonkey, How would I do that? Can you explain please. |
|
|
|
|
|
BigBear
|
Posted: Thu Apr 02, 2009 7:33 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
so you have a for loop
for i : 1.. length(word)
then check to see if word(i) = " "
then check the value of i so if i is = 5 then it is a four letter word |
|
|
|
|
|
Sponsor Sponsor
|
|
|
CodeMonkey2000
|
Posted: Thu Apr 02, 2009 7:39 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
Well, initially the word length is assumed to be 0. You keep incrementing the length of the word until you hit a space. At that point you know the length of that word. |
|
|
|
|
|
Houdini
|
Posted: Thu Apr 02, 2009 7:58 pm Post subject: Re: How to count 2 lettered words in sentence |
|
|
Here is what I have now, I don't understand why its not working.
Turing: |
var sentence : string
var words : int := 1
var chars: int := 0
put "Please type in a sentence and hit enter"
get sentence : *
for x : 1 .. length (sentence )
chars := chars + 1
if sentence (x ) = " " then
if chars = 2 then
words: = words + 1
end if
end if
end for
put "There is a total of ", words, " 2-letter words"
|
|
|
|
|
|
|
saltpro15
|
Posted: Thu Apr 02, 2009 8:30 pm Post subject: Re: RE:How to count 2 lettered words in sentence |
|
|
methodoxx @ Thu Apr 02, 2009 wrote: Turing: | if counter >1 and counter <3 then |
Seriously?? I hope that was just a brain fart.
my bad, it's been a long day... |
|
|
|
|
|
Houdini
|
Posted: Thu Apr 02, 2009 9:06 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
Damn, still not working o.0 |
|
|
|
|
|
BigBear
|
Posted: Thu Apr 02, 2009 9:59 pm Post subject: RE:How to count 2 lettered words in sentence |
|
|
It only check if it = 2 once so if there is another 2 letter word afer that but not before a space at position 2 it won't count |
|
|
|
|
|
CodeMonkey2000
|
Posted: Thu Apr 02, 2009 10:20 pm Post subject: Re: How to count 2 lettered words in sentence |
|
|
Houdini @ Thu Apr 02, 2009 7:58 pm wrote: Here is what I have now, I don't understand why its not working.
Turing: |
var sentence : string
var words : int := 1
var chars: int := 0
put "Please type in a sentence and hit enter"
get sentence : *
for x : 1 .. length (sentence )
chars := chars + 1
if sentence (x ) = " " then
if chars = 2 then
words: = words + 1
end if
end if
end for
put "There is a total of ", words, " 2-letter words"
|
You are getting close. The length of a word increases only when the current character isn't a space. Also once you are done with a word (you hit a space), you aren't resetting the word length. Follow what happens to the chars variable closely. Output it if you have to. |
|
|
|
|
|
Dusk Eagle
|
Posted: Thu Apr 02, 2009 10:41 pm Post subject: Re: How to count 2 lettered words in sentence |
|
|
Well I typed this all up before CodeMonkey2000 responded, but seeing as I make a few different points than him I'm going to post this anyway.
The first problem with the code posted above is that you've initialized the value of words to 1 even though at that point you should have zero two letter words. This results in an Off-By-One Error. Your second problem is that the value of chars will always be three if you have a two letter word, as you increment the value of 'char' by one before checking if your current character is an " ". Therefore, the whitespace gets counted as an extra character. Your third problem is that you never reset the value of chars to zero. So although this code will work for the first word entered it will not work for any subsequent one.
Fixing these three errors should get your program up and running for the most part. However, you will still have some minor problems with the following:
The last word of your string. The last word will not have a " " following it so your program will never actually check the length of it. This could be solved by automatically appending a space to the end of the string. Your other minor problem is punctuation. A two-letter word followed by a comma or period, for example, will appear to the program as three letters (similarly, a one letter word with a single punctuation mark will appear as a two letter-word). To solve this, you could try checking the value of sentence(x) before incrementing chars by one. Something like the following should do:
Turing: |
if (sentence(i) >= 'A' and sentence (i) <= 'Z') or (sentence(i) > 'a' and sentence(i) < 'z') then
chars+=1
end if
|
Hope this helps! |
|
|
|
|
|
|