You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
How do I read a list of name, and then tell which one is
the longest. And, how can I get the longest number of letters, in a
name, changed to the Name of the letters, example: Dan 3.
Reading the names is simple, but how it is done depends on where the list resides. For example, if the list is in a disk (text)file, with one name per line, then the following code will work fine:
'names.txt would loook like this:
DAN
BRUCE
DAVE
SAMSON
GODFREY
Al
...etc
DIM A$(1:1000) ' maxmimum 1000 names
x% = FREEFILE
OPEN "NAMES.TXT" FOR INPUT AS #x%
y% = 0
WHILE NOT EOF(x%)
INCR y%
LINE INPUT #x%, A$(y%)
WEND
CLOSE #1
TO sort the list into alphanumeric order is simple too:
ARRAY SORT A$() FOR y%
Now, find the longest name (in characters).
z% = 0
FOR x% = 1 to y%
z% = MAX%(z%, len(A$(x%))
NEXT x%
At this point, z% contains the array subscript number of the name with the most letters.
PRINT "The longest name is " + A$(z%) + " and it is " + LEN(A$(z%)) + " characters long."
I've kept the example code very simple here. In a real application you may want to combine the length-checking code into the file-read loop. Also, I've not added any error checking to verify the file I/O operations (or is the names list file even exists!), or checked if the names list exceeds the limit of the array - in a real application all of these things would need to be taken into account. Also, the code would need to handle the situation where no names existed in the list...
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment