As you will see by the code I will post I'm trying to get a short and long count. I have been looking at this site and other sites I can't figure out how to do it. As you can guess by my coding I'm a total beginner and I'm still trying to understand what to use and when to use it, so please keep it basic. So here is what I have so far.
And Please use CODE tags to enclose your code.
This code doesn't count the number of short words and long words. The short words variable is a count of the number of characters in the string, whose ASCII value is less than or equal to the ASCII value for the character "4". The long words variable is a count of the number of characters in the string, whose ASCII value is greater than or equal to the ASCII value for the character "5".
Alfonso Saballett wrote:So if I'm understanding you correctly if I run this program and I don't have a "4" or "5" in the input, it won't count it but when I run it by typing in, "This is a test. This is a test." I get the result of 9 shorts words and 22 long words obviously this is wrong but from what you are saying should it be 0 shorts words and 0 long words? I appreciate the help and patients. It was suggested earlier that I use the split() method, how?
Nope... here is an ASCII table...
Basically, all number digits less than 4, along with the spaces and other punctuations, will count as a short word. All number digits greater than 5, along with the alphabet will count as a long word. See the table for what counts as what.
To give more detail, your program is reporting 9 spaces and periods, and 22 letters.
Run this and tell us do you find any difficulties understanding this code snippet?
Just throwing a monkey wrench into this.... I am assuming that this is a homework problem, for a beginner's class. And as such, I am assuming the instructor is expecting you to loop through the characters of the string -- To determine what is a whitespace versus what is a letter. To determine whether you are in a word or not. To determine whether you are in a sentence or not... While keeping count of the size of the words too.
Using the regular expression engine to do this may not be allowed by your instructor.
Vishal Pandya wrote:Henry, Do you want me to remove that code snippet? OP has nowhere mentioned about Split method that it is not allowed?
No. There is nothing wrong with the post. I just wanted to make sure that the OP don't use a library, that technically, haven't learn to use yet.
Yes Henry, your assumptions are correct but I think it would be OK for to use it as an advance student use BreakIterator and that has not been cover but he was fine with it.
I ran the code snippet and it gave me the back what I had enter. I can use the split() method but I don't understand the following snippet of code:
I understand the for loop that I have used in my code but not this type. Thanks for the example it has help me understand the split() method better. I will try to figure out how I can use it to count the small and large words.