Small Basic Forum
Challenge 5 - Count words and lines - Printable Version

+- Small Basic Forum (https://litdev.uk/mybb)
+-- Forum: Small Basic (https://litdev.uk/mybb/forumdisplay.php?fid=1)
+--- Forum: Challenges (https://litdev.uk/mybb/forumdisplay.php?fid=5)
+--- Thread: Challenge 5 - Count words and lines (/showthread.php?tid=125)



Challenge 5 - Count words and lines - litdev - 09-06-2024

Write a program to count the number of lines and words in a text document - no extensions.


RE: Challenge 5 - Count words and lines - z-s - 09-06-2024

I currently not on pc but I will help.
1) load file using file = file.readlines
2) use text.split for the lines ex: countlines = text.split(file,"/n")
3) use text.split for words ex: countwords = text.split(file," ") use space as separator as words will be separated by space
4) get the length of array countlines and countwords and print on text window.

filep = File.ReadContents(filePath)
text = Array.GetItemCount(filep)
TextWindow.WriteLine(text)


For lines


RE: Challenge 5 - Count words and lines - jrmrhrb00 - 09-11-2024

In a way this seems like an easy challenge, but when you get into it, it is tough! The easy part says just use spaces in between words and count them. For lines just count line feed characters. 

Then you write out a document with Notepad. You write a sentence and terminate it with a period. Then write another sentence and terminate it with a question mark and so on. Maybe you throw in an exclamation point or 2. In your sentences you throw in some numbers. Like I just did. Those are not really words. Do they count? Your sentences don't have linefeed characters. Do they count?

So, my thinking is maybe we should have a little more guidance for this challenge! I have noticed that there is only one reply to this challenge. Maybe this is why.

JR


RE: Challenge 5 - Count words and lines - litdev - 09-12-2024

Yep, not so simple - remember I said no extensions, but with extensions I would use Regex and this is a great thing to know about.

I would decide first if we are going to:

a) define word characters and everything else is a word deliminator, or
b) define word deliminators and everthing else is a word character

I would go with the first, maybe:

lower and upper case letters (a-zA-Z) and numbers (0-9) and perhaps hypen and underscore.

To do this I would have a subroutine that sets a flag as word or not for any character, and I would probably use ascii character codes for this.

Just a thought - to handle other languages I might go with option b.


RE: Challenge 5 - Count words and lines - jrmrhrb00 - 09-13-2024

Here it is: FLBR979.000

This was a real challenge by not using extensions. It would have been easier in c#. I'm sure someone can do it better.

JR


RE: Challenge 5 - Count words and lines - AbsoluteBeginner - 09-13-2024

(09-13-2024, 12:00 PM)jrmrhrb00 Wrote: ...

This was a real challenge by not using extensions. It would have been easier in c#...

JR

This is the same advantage that Small Basic has over C# as the LEGO constructor has over professional industrial equipment.  Cool

No other tool will ever take away from the “Small Basic + LD expansion + SB-Prime” set the ability to so easily give a person the joy of success.  Tongue


RE: Challenge 5 - Count words and lines - litdev - 09-13-2024

JCMV471.000-1


RE: Challenge 5 - Count words and lines - jrmrhrb00 - 09-13-2024

LitDev,

Interesting in your program shows a different way of doing it. I wonder if there are other ways.

JR