Legitimate file extension checker utilizing Routine Expression

Enhance Short Article

Conserve Short Article

Like Short Article

Enhance Short Article

Conserve Short Article

Like Short Article

Offered string str, the job is to inspect whether the provided string is a legitimate file extension or not by utilizing Routine Expression

The legitimate file extension should define the list below conditions:

  • It needs to begin with a string of a minimum of one character.
  • It needs to not have any white area.
  • It needs to be followed by a dot(.).
  • It needs to end with any among the file extensions:
    ex: jpg, txt, mp3, png, ppt,. and so on

Examples:

Input: str = document.txt
Output: text
Description: Here the string extends.txt then it is a text file.

Input: str = picture.jpg
Output: image
Description: Here the string extends.jpg then it is an image file.

Below are the actions associated with the application of the code:

  • Get the String
  • Transformed the string to lowercase utilizing the lower approach, in case it remains in uppercase.
  • Extract the extension of the substring beginning with the position of the ‘.’ character to the end of the string.
  • If the extension matches among the recognized extensions, the function returns a string showing the kind of the file (e.g. ‘image’ for image files, ‘audio’ for audio files, and so on). If the extension does not match any of the recognized extensions, the function returns ‘unidentified’.
  • print the file name.

Below is the application of the above technique:

C++

// C++ code for the above technique:

. #include < bits/stdc++. h>
> 
. utilizing namespace sexually transmitted disease;

.

. string get_file_type( string filename) 
.
{
. 
.// Transform the string to lowercase 
. change( filename.begin(), filename.end(),

. filename.begin(),:: tolower);

.

.// Get the substring after (.)

.
string ext 
.= filename.substr( filename.find _ last_of ("." )); 
. if( ext == ". txt"|| ext == ". csv"|
|
ext==". 
tsv"
) {
. return "text"
;

.} 
. else if (ext==". png "||
ext== ".
jpg" 
.
||
ext== ".
jpeg"|
.
| ext= =". gif ") {
.
return" image"; 
.} 

. else if( ext=
=
". 
pdf"
) {
. return "pdf"; 
.} 
.
else if( ext == ". mp3"|
|
ext==". 
wav"

.|| ext == ". wma") {

. return" audio"
;

.
} 
. else if( ext == ". mp4"|
| ext==".
avi"|
.
| ext= =". mov "
.|| 
ext== ". wmv")
{

. return" video"; 
.} 
.
else if( ext == ". doc"|
|
ext==". 
docx"
) {
. return "file";

.}

.
else if (ext == ". xls"|| ext == ". xlsx" ){
.
return" spreadsheet"; 
.} 

. else if( ext==".
ppt"|
|
ext==". 
pptx ") {

.
return" discussion";


.}

.
else if( ext==". 
zip "| |

ext=
=". rar" 
.
||ext = =".7 z" ){
. return "archive";

. <<}

.
else if (ext = =". exe"|| ext = =". msi") {
. return "executable"; 
.} 
. else if (ext = =". java"|| ext = =". c"|| ext = =". py" 
.|| ext = =". cpp") {
. return "code"; 
.} 
. else {
. return "unidentified"; 
.} 
.} 
. 
.// Chauffeur code 
. int primary( )
. {
. string str="document.txt"; 
. 
.// Function call 
. string ans = get_file_type (str); 
. cout < < ans < < endl; 
. return 1; 
.}

Time Intricacy: O( 1)
Auxiliary Area: O (1)

Associated Articles:

Last Upgraded:
11 May, 2023

Like Short Article

Conserve Short Article

.

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: