Delphi programming challenges / exercises are designed to help you refactor your old code to make it more efficient; get new ideas on how to code faster / better Delphi code; help you solve a particular Delphi programming task and of course: to have fun while coding in Delphi :)
Challenge: ExtractBasePathYour challenge is to code a custom Delphi function with the following signature:
ExtractBasePath takes 2 path names (directory or file) and should return the base / common path for the paths provided.
function ExtractBasePath(const path1, path2 : string) : string;
Note: the function should not raise any errors even if the provided paths do not exist.
For example: given the two paths "c:\code\examples\test.pas" and "c:\code\challenge.dfm", the ExtractBasePath should return "c:\code\"
Note: for the following two paths "c:\folder\code\file.pdf" and "c:\folder\comet\document.doc" the common base path is "c:\folder" and NOT "c:\folder\co". This means that the result must represent a folder / path name.
If the two paths provided do not have a common base path the function should return an empty string.
For example: "c:\folder\file.doc" and "d:\directory\document.pdf" do NOT have a common base path - as one file is on the C drive and another file is on the D drive.
Enter ChallengeNote: This is for glory only. About.com does not permit prizes to be given.
To "win" the challenge you obviously need to submit a function that solves the given problem. Also, make sure the code is "nice", has no memory leaks and preferably does not have hundreds of lines.
Selected entries will be listed. A condition of entry is that you allow your source code to be published on this website, with full credits to you as the author.
ExtractBasePath : Closed!Note: this challenge is closed. 12 valid entries have been submitted.
ExtractBasePath : Accepted EntriesHere's the list of the accepted entries. In total 12 entries were accepted before the challenge was closed.
Some implementations use TStringList, some pointers, some use iterating through paths one character at a time, some have less than 10 lines, some have more than 30 lines, one even uses assembler. Common to entries is the usage of the RTL routines.
Note: the list is in random order.
Participate in active Delphi programming challenges
ExtractBasePath by Boris Kumpar ExtractBasePath by Pablo Anizio ExtractBasePath by Vlad Man ExtractBasePath by Josip Brozovic ExtractBasePath by Korhan Taþçýkar ExtractBasePath by Jeff Lawson ExtractBasePath by Ivan Cvetkovic ExtractBasePath by Paul Bennett ExtractBasePath by Caleb Hattingh ExtractBasePath by Ricardo de O. Soares ExtractBasePath by Antonio Bakula ExtractBasePath by Jens Borrisholt