Given string str, the task is to check whether the given string is a valid file extension or not by using Regular Expression.
The valid file extension must specify the following conditions:
- It should start with a string of at least one character.
- It should not have any white space.
- It should be followed by a dot(.).
- It should end with any one of the file extensions:
ex : jpg, txt, mp3, png, ppt, .. etc
Examples:
Input: str = document.txt
Output: text
Explanation: Here the string extends .txt then it is a text file.
Input: str = picture.jpg
Output: picture
Explanation: Here the string extends .jpg then it is a picture file.
Below are the steps involved in the implementation of the code:
- Get the String.
- Converted the string to lowercase using the lower method, in case it is in uppercase.
- Extract the extension of the substring starting from the position of the '.' character to the end of the string.
- If the extension matches one of the known extensions, the function returns a string indicating the type of the file (e.g. 'image' for image files, 'audio' for audio files, etc.). If the extension doesn't match any of the known extensions, the function returns 'unknown'.
- print the file name.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Related Articles: