How to resolve the algorithm Ordered words step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ordered words step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

An   ordered word   is a word in which the letters appear in alphabetic order. Examples include   abbey   and   dirt. Find and display all the ordered words in the dictionary   unixdict.txt   that have the longest word length. (Examples that access the dictionary file locally assume that you have downloaded this file yourself.)
The display needs to be shown on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ordered words step by step in the FreeBASIC programming language

Source code in the freebasic programming language

' FB 1.05.0 Win64

Function isOrdered(s As Const String) As Boolean
  If Len(s) <= 1 Then Return True
  For i As Integer = 1 To Len(s) - 1
    If s[i] < s[i - 1] Then Return False
  Next 
  Return True
End Function

Dim words() As String
Dim word As String
Dim maxLength As Integer = 0
Dim count As Integer = 0 
Open "undict.txt" For Input As #1
While Not Eof(1)
  Line Input #1, word
  If isOrdered(word) Then
    If Len(word) = maxLength Then
      Redim Preserve words(0 To count)
      words(count) = word
      count += 1
    ElseIf Len(word) > maxLength Then
      Erase words
      maxLength = Len(word)
      Redim words(0 To 0)
      words(0) = word
      count = 1
    End If
  End If
Wend

Close #1

Print "The ordered words with the longest length ("; Str(maxLength); ") in undict.txt are :"
Print
For i As Integer = 0 To UBound(words)
  Print words(i)
Next
Print
Print "Press any key to quit"
Sleep

  

You may also check:How to resolve the algorithm Zumkeller numbers step by step in the Go programming language
You may also check:How to resolve the algorithm Comments step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Anadromes step by step in the jq programming language
You may also check:How to resolve the algorithm Fast Fourier transform step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Chowla numbers step by step in the Julia programming language