We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge 5 - Count words and lines
#1
Write a program to count the number of lines and words in a text document - no extensions.
Reply
#2
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
ZS
Reply
#3
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
[-] The following 1 user Likes jrmrhrb00's post:
  • litdev
Reply
#4
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.
[-] The following 1 user Likes litdev's post:
  • AbsoluteBeginner
Reply
#5
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
[-] The following 2 users Like jrmrhrb00's post:
  • AbsoluteBeginner, litdev
Reply
#6
(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
Reply
#7
JCMV471.000-1
[-] The following 1 user Likes litdev's post:
  • jrmrhrb00
Reply
#8
LitDev,

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

JR
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)