Extract file extension: Difference between revisions

Content added Content deleted
(→‎{{header|Forth}}: Add Fortran.)
(→‎Task: improve formatting and wording)
Line 1: Line 1:
{{draft task}}
{{draft task}}


{{task heading}}
Write a program that takes one string argument representing the path to a file and returns the file's extension, or the null string if the file path has no extension.


Write a function or program that
An extension appears after the last period in the file name and consists of one or more letters or numbers.
* takes one string argument representing the path/URL to a file
* returns the file's extension, or an empty string if it has no extension.


{{task heading|Details}}
Show here the action of your routine on the following examples:


* An extension appears after the last period in the file's name, and consists of one or more letters or numbers.
# picture.jpg returns .jpg
* The file name is separated from preceding directory parts, if any, by a forward slash.
# <nowiki>http://mywebsite.com/picture/image.png</nowiki> returns .png

# myuniquefile.longextension returns .longextension
{{task heading|Test cases}}
# IAmAFileWithoutExtension returns an empty string ""

# /path/to.my/file returns an empty string as the period is in the directory name rather than the file
{| class="wikitable"
# file.odd_one returns an empty string as an extension (by this definition), cannot contain an underscore.
|-
<br><br>
! Input
! Output
! Comment
|-
| <code>picture.jpg</code>
| <code>.jpg</code>
|
|-
| <code><nowiki>http://mywebsite.com/picture/image.png</nowiki></code>
| <code>.png</code>
|
|-
| <code>myuniquefile.longextension</code>
| <code>.longextension</code>
|
|-
| <code>IAmAFileWithoutExtension</code>
| <code></code>
| ''empty string''
|-
| <code>/path/to.my/file</code>
| <code></code>
| ''empty string, as the period is in the directory name rather than the file name''
|-
| <code>file.odd_one</code>
| <code></code>
| ''empty string, as an extension (by this definition), cannot contain an underscore''
|}

<hr>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==