How to resolve the algorithm Find common directory path step by step in the PureBasic programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Find common directory path step by step in the PureBasic programming language
Table of Contents
Problem Statement
Create a routine that, given a set of strings representing directory paths and a single character directory separator, will return a string representing that part of the directory tree that is common to all the directories. Test your routine using the forward slash '/' character as the directory separator and the following three strings as input paths: Note: The resultant path should be the valid directory '/home/user1/tmp' and not the longest common string '/home/user1/tmp/cove'. If your language has a routine that performs this function (even if it does not have a changeable separator character), then mention it as part of the task.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Find common directory path step by step in the PureBasic programming language
Source code in the purebasic programming language
Procedure.s CommonPath(Array InPaths.s(1),separator.s="/")
Protected SOut$=""
Protected i, j, toggle
If ArraySize(InPaths())=0
ProcedureReturn InPaths(0) ; Special case, only one path
EndIf
Repeat
i+1
toggle=#False
For j=1 To ArraySize(InPaths())
If (StringField(InPaths(j-1),i,separator)=StringField(InPaths(j),i,separator))
If Not toggle
SOut$+StringField(InPaths(j-1),i,separator)+separator
toggle=#True
EndIf
Else
ProcedureReturn SOut$
EndIf
Next
ForEver
EndProcedure
Dim t.s(2)
t(0)="/home/user1/tmp/coverage/test"
t(1)="/home/user1/tmp/covert/operator"
t(2)="/home/user1/tmp/coven/members"
Debug CommonPath(t(),"/"))
You may also check:How to resolve the algorithm User input/Graphical step by step in the Kotlin programming language
You may also check:How to resolve the algorithm MAC vendor lookup step by step in the Ruby programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the Ursa programming language