Posted: Wed May 11, 2005 12:07 pm Post subject: Sentence Count
Hello All .. This will be my first post soo i'd like to say hi before i actualy ask for help .. i'll try and contribute more to the forums .. but i'v started something new in my class that .. im not to clear on .. ok ok enough of the small talk here's my question:
I have to make a word count application which allows the user to enter a sentence and then count and display the number of words entered.
Extension - Analyze the paragraph by not only counting the words but by displaying the number of 1 character words, 2 character words, 3 character words
this is what i got so far all i need help with is getting the 1..2..3 charcacter words
code:
var sentence : string
put "Word Count"
put "Please enter a sentence to count how many letters are in it"
get sentence : *
put "The Length Of Your Sentence Is " , length(sentence)
Sponsor Sponsor
jamonathin
Posted: Wed May 11, 2005 1:22 pm Post subject: (No subject)
Since its your first post and you dont know, its ok, but we don't do peoples homework on here, we can give you clues and tips, but we wont write you all of the actual code.
Anyways, think of how a sentence is made. Inbetween each word there's a space, unless it's the end of the sentence. Run a for loop for the length(sentence) and have it check where the spaces are, and how far apart, thats how you can find out where the words are.
sockoo
Posted: Wed May 11, 2005 7:24 pm Post subject: (No subject)
Thank you, i didnt want you to write me my code .. what would i learn that way ? .. i just wanted a sorce of direction or help of how to accomplish what i was giving and thanks for the help .. i'll post back with furthur updates or .. if i need any more help
Cervantes
Posted: Thu May 12, 2005 3:20 pm Post subject: (No subject)
If you're not too comfortable with string manipulation, check the tutorial to get some explainations and examples.
Ultimately, you don't want to count the number of spaces. The reason is that lots of people (myself included) leave two spaces after the end of my sentances. If you're only searching for spaces, if the user types this way, the output for the number of words will be incorrect. What you actually want to do (I would assume) is search for characters between spaces.
sockoo
Posted: Fri May 13, 2005 8:52 am Post subject: (No subject)
im still not having much luck .. i understand how to find where the space is but .. how would i go about finding .. what many characters are before the space ?
code:
for i : 1 .. length(sentence)
if sentence(i) = " " then
this is what i have but .. im not clear what to do after this obviously i'v found where the space is soo would i find the length of the sentence 1 .. i ? to find out how many charcaters is in the word ? .. and run if statements 1 .. 3 to find if it has 1 charc 2 charcs or 3 and so on ? ..
jamonathin
Posted: Sun May 15, 2005 7:30 pm Post subject: (No subject)
Here's the answer you want. I'm guessing you dont have it by now since you haven't posted it.
Turing:
var sentence :string put"Enter a Sentence" get sentence :*%Getting sentence with spaces ( : * ) var edit :string:=" " + sentence %Adding a space to the begining of the sentence var words :flexiblearray1.. 0ofstring%All of the words procedure wordcount (s :string) for i :1.. length(s) if s (i)=" "then%If there's a space new words, upper(words) + 1%making the array 'words' 1 larger
words (upper(words)):=""%Making it equal to nothing so we can add to it for q : i + 1.. length(s)%Add whatever other characters to the if statement as you please if s (q)=" "or s (q)=","or s (q)="."or s (q)="?"or s (q)="!"then exit%Exiting the for loop else
words (upper(words)) += s (q)%Adding to the current word endif endfor put words (upper(words))%Displaying the word endif endfor end wordcount
cls
wordcount (edit)
sockoo
Posted: Sun May 15, 2005 7:35 pm Post subject: (No subject)
hmm thanks for the help but that isnt quiet what i was trying to ahieve ...
lets say the user enters " Keep It Simple Stupid "
sentence length: 22 (with spaces)
4 charc words: 1
2 charc words: 1
6 charc words: 2
MysticVegeta
Posted: Mon May 16, 2005 7:03 am Post subject: (No subject)
Check the Cervantes link, He has a part in the tut that gives how you can separate the words, and then you can go -> if length(seperateword) = 2 then
put 2
end if
Since i cant post the code, i will ask yu to check it out
http://www.compsci.ca/v2/viewtopic.php?t=8229
Sponsor Sponsor
jamonathin
Posted: Mon May 16, 2005 7:48 am Post subject: (No subject)
Oh yeah I forgot about that tutorial, definately check it out, it has everything you need to know.
sockoo
Posted: Mon May 16, 2005 9:10 am Post subject: (No subject)
i'v read that tutorial over about 5 times ... but i'll re read it when i get home .. and go from there . thanks for the help guys
Notoroge
Posted: Mon May 16, 2005 2:34 pm Post subject: (No subject)
code:
var asdf, yummy : string
var countwords : int := 1
get asdf : *
yummy := asdf
asdf := "+" + yummy + " "
put "Sentence is ", length (asdf) - 2, " characters long."
for i : 1 .. length (asdf)
if asdf (i) = " " then
countwords := countwords + 1
end if
end for
countwords := countwords - 1
put "There are ", countwords, " words in the sentence."
var words : array 1 .. countwords of string
var count, temp : int := 1
for i : 1 .. length (asdf)
if asdf (i) = " " then
words (count) := asdf (temp + 1 .. i - 1)
count := count + 1
temp := i
end if
end for
temp := 0
for i : 1 .. countwords
count := length (words (i))
if count > temp then
temp := count
end if
end for
var lengths : array 1 .. temp of int
for i : 1 .. temp
lengths (i) := 0
end for
for i : 1 .. countwords
for r : 1 .. temp
if length (words (i)) = r then
lengths (r) := lengths (r) + 1
end if
end for
end for
for i : 1 .. temp
if lengths (i) not= 0 then
put "There are ", lengths (i), ", ", i, " lettered words."
end if
end for
It's not elegant; and probably the dirtiest way to do it, but it took me ten minutes to make and it works.
Update: It does crash when you put two or more spaces consecutively. This is only to point you in the right direction though. Kinda the concept of what you have to do. Fix it however you want.
Notoroge
Posted: Mon May 16, 2005 2:46 pm Post subject: (No subject)
jamonathin, I just thought of something. Instead of trapping for most commonly used punctuation notes, why not do something like,
code:
if ord (word : string) => [lower bound ASCII limit of letters] : int and ord (word : string) =< [upper bound ASCII limit of letters] : int then
Just a thought.
Token
Posted: Mon May 16, 2005 3:28 pm Post subject: (No subject)
hmm i just got an idea, why not export the sentence to a text file and then bring it in one word at a time, that way you could ealsily count the words, and as each word is in the temporary variable that you set up it could count the number of letters. if u dont understand what i'm saying or u need help let me kno
sockoo
Posted: Mon May 16, 2005 4:58 pm Post subject: (No subject)
Notorage , Omg! , u'v made my program to a dot .. everything it was suppose to be .. except i learned nothing from it .. even tho it was nice of you .. i would of rather a source of direction how to do it .. rather then u just giving me the answer .. but thank you ! , now gotta read through ur code and make sure i understand everything you did and would be able to do it my self .. if needed in the furture!
Notoroge
Posted: Mon May 16, 2005 5:03 pm Post subject: (No subject)
I wanted to see if I could do it. I also purposefully left out any documentation and didn't bother to clean up the code or error-trap. Because I figured I'd might aswell make you read through everything, understand it, and clean it up yourself.