How to resolve the algorithm Odd word problem step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Odd word problem step by step in the F# programming language

Table of Contents

Problem Statement

Write a program that solves the odd word problem with the restrictions given below.

You are promised an input stream consisting of English letters and punctuations.
It is guaranteed that:

A stream with six words:

The task is to reverse the letters in every other word while leaving punctuations intact, producing: while observing the following restrictions:

Work on both the   "life"   example given above, and also the text:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Odd word problem step by step in the F# programming language

Source code in the fsharp programming language

open System
open System.Text.RegularExpressions

let mutable Inp = Console.In

let Out c = printf "%c" c; (if c = '.' then Environment.Exit 0)

let In() = Inp.Read() |> Convert.ToChar

let (|WordCharacter|OtherCharacter|) c =
    if Regex.IsMatch(c.ToString(),"[a-zA-Z]") then
        WordCharacter
    else
        OtherCharacter

let rec forward () =
    let c = In()
    let rec backward () : char =
        let c = In()
        match c with
        | WordCharacter ->
            let s = backward() in Out c; s
        | OtherCharacter -> c
    Out c
    match c with
    | WordCharacter -> forward()
    | OtherCharacter -> backward() 

[<EntryPoint>]
let main argv =
    if argv.Length > 0 then Inp <- new System.IO.StringReader(argv.[0])
    let rec loop () = forward() |> Out;  loop()
    loop()
    0


  

You may also check:How to resolve the algorithm Set of real numbers step by step in the Haskell programming language
You may also check:How to resolve the algorithm Video display modes step by step in the Julia programming language
You may also check:How to resolve the algorithm Function definition step by step in the V programming language
You may also check:How to resolve the algorithm Abbreviations, easy step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Latin Squares in reduced form step by step in the jq programming language