How to resolve the algorithm Reverse words in a string step by step in the VBScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Reverse words in a string step by step in the VBScript programming language

Table of Contents

Problem Statement

Reverse the order of all tokens in each of a number of strings and display the result;   the order of characters within a token should not be modified.

Hey you, Bub!   would be shown reversed as:   Bub! you, Hey

Tokens are any non-space characters separated by spaces (formally, white-space);   the visible punctuation form part of the word within which it is located and should not be modified. You may assume that there are no significant non-visible characters in the input.   Multiple or superfluous spaces may be compressed into a single space. Some strings have no tokens, so an empty string   (or one just containing spaces)   would be the result. Display the strings in order   (1st, 2nd, 3rd, ···),   and one string per line. (You can consider the ten strings as ten lines, and the tokens as words.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Reverse words in a string step by step in the VBScript programming language

Source code in the vbscript programming language

Option Explicit

Dim objFSO, objInFile, objOutFile
Dim srcDir, line

Set objFSO = CreateObject("Scripting.FileSystemObject")

srcDir = objFSO.GetParentFolderName(WScript.ScriptFullName) & "\"

Set objInFile = objFSO.OpenTextFile(srcDir & "In.txt",1,False,0)

Set objOutFile = objFSO.OpenTextFile(srcDir & "Out.txt",2,True,0)

Do Until objInFile.AtEndOfStream
	line = objInFile.ReadLine
	If line = "" Then
		objOutFile.WriteLine ""
	Else
		objOutFile.WriteLine Reverse_String(line)
	End If
Loop

Function Reverse_String(s)
	Dim arr, i
	arr = Split(s," ")
	For i = UBound(arr) To LBound(arr) Step -1
		If arr(i) <> "" Then
			If i = UBound(arr) Then
				Reverse_String = Reverse_String & arr(i)
			Else
				Reverse_String = Reverse_String & " " & arr(i)
			End If
		End If
	Next
End Function

objInFile.Close
objOutFile.Close
Set objFSO = Nothing

  

You may also check:How to resolve the algorithm Create a file step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Factorions step by step in the OCaml programming language
You may also check:How to resolve the algorithm Include a file step by step in the OASYS Assembler programming language
You may also check:How to resolve the algorithm Hickerson series of almost integers step by step in the Sidef programming language
You may also check:How to resolve the algorithm Memory layout of a data structure step by step in the Forth programming language