Letter frequency: Difference between revisions

Content added Content deleted
(Added Wren)
Line 4,019: Line 4,019:


Again eliminating all fussing with the details of converting letters into list indices.
Again eliminating all fussing with the details of converting letters into list indices.

=={{header|Quick Basic PDS 7.1/VB-DOS}}==
This version only counts valid letters from A to Z (including Ñ in Spanish alphabet). Takes in account accented vowels. It runs in QB PDS 7.1 and VB_DOS. If you want to run it in QBASIC or Quick BASIC 4.x, you need to modify the error handler.
<lang VB>
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' Program CountLet '
' '
' This program counts how many letters have a text '
' file specified by the user. Also, returns how many '
' total characters has the file. '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' OPTION EXPLICIT ' Remove comment in VB-DOS

' Var
DIM iFile AS INTEGER
DIM i AS INTEGER
DIM iLetter AS INTEGER
DIM iMUL AS INTEGER
DIM iMUI AS INTEGER
DIM iLUL AS INTEGER
DIM iLUI AS INTEGER
DIM iP AS INTEGER
DIM lHowMany AS LONG
DIM lTotLetters AS LONG
DIM strChar AS STRING
DIM strFile AS STRING
DIM strTxt AS STRING
DIM strTime AS STRING

'------Main program cycle
strTime = TIME$
iFile = FREEFILE
DO
REDIM iL(0 TO 25) AS INTEGER ' 25 is ¤
iMUL = 0
iMUI = 0
CLS
INPUT ; "File to open: ", strFile
ON ERROR RESUME NEXT
OPEN strFile FOR INPUT AS #iFile
IF ERR <> 53 THEN
iP = POS(0)
lHowMany = LOF(iFile)
PRINT " File is"; STR$(lHowMany); " in size. Working"; : COLOR 23: PRINT "..."; : COLOR (7)
DO WHILE NOT EOF(iFile)
LINE INPUT #iFile, strTxt
FOR i = 1 TO LEN(strTxt)
strChar = UCASE$(MID$(strTxt, i, 1))
SELECT CASE strChar
CASE CHR$(164): strChar = CHR$(165) ' Ñ
CASE CHR$(160): strChar = "A" ' á
CASE CHR$(130), CHR$(144): strChar = "E" ' é, É
CASE CHR$(161): strChar = "I" ' í
CASE CHR$(162): strChar = "O" ' ó
CASE CHR$(163), CHR$(129): strChar = "U" ' ú, ü
END SELECT
iLetter = ASC(strChar) - 65
IF iLetter >= 0 AND iLetter <= 25 THEN
iL(iLetter) = iL(iLetter) + 1
ELSEIF iLetter = 100 THEN ' Ñ
iL(25) = iL(25) + 1
END IF
NEXT i
LOOP

' Show the letters found
iMUL = 0
iMUI = 0
iLUL = 32767
iLUI = 0
lTotLetters = 0
LOCATE , iP: PRINT SPACE$(40): PRINT
PRINT "Letters found: ";
FOR i = 0 TO 24
' Most Used Letter
IF iMUL < iL(i) THEN
iMUL = iL(i)
iMUI = i
END IF

' Print letter
IF iL(i) > 0 THEN
PRINT CHR$(i + 65); "="; LTRIM$(STR$(iL(i)));
IF i < 24 THEN PRINT ", ";
IF POS(0) > 73 THEN PRINT
lTotLetters = lTotLetters + iL(i)

' Least Used Letter
IF iLUL > iL(i) THEN
iLUL = iL(i)
iLUI = i
END IF
END IF
NEXT i

IF iL(25) > 0 THEN
' Are there more ¤s?
IF iMUL < iL(25) THEN
iMUL = iL(25)
iMUI = 25
ELSEIF iLUL > iL(25) THEN
iLUL = iL(25)
iLUI = 25
END IF
PRINT ", "; CHR$(165); " ="; STR$(iL(25));
END IF
PRINT "."

' Shows the most used letter
IF iMUI < 25 THEN strChar = CHR$(iMUI + 65) ELSE strChar = CHR$(165)
IF iLUI < 25 THEN strTxt = CHR$(iLUI + 65) ELSE strTxt = CHR$(165)
PRINT
PRINT "Total characters in document:"; lHowMany
PRINT "Total letters in document...:"; lTotLetters
PRINT "Other characters in document:"; lHowMany - lTotLetters
PRINT "Most used letter was........: "; strChar
PRINT "Least used letter was.......: "; strTxt
ELSE
' File does not exist
ON ERROR GOTO 0
PRINT
PRINT "File does not exist."
END IF
CLOSE #iFile

' Again?
PRINT
PRINT "Again? (Y/n)"
DO
strChar = UCASE$(INKEY$)
LOOP UNTIL strChar = "N" OR strChar = "Y" OR strChar = CHR$(13) OR strChar = CHR$(27)
LOOP UNTIL strChar = "N" OR strChar = CHR$(27)

CLS
PRINT "End of execution. Start time: "; strTime; ", end time: "; TIME$; "."
END
' ---End of main program cycle
</lang>



=={{header|R}}==
=={{header|R}}==