Given string str, the duty is to verify whether or not the given string is a sound file extension or not through the use of Common Expression.
The legitimate file extension should specify the next situations:
- It ought to begin with a string of a minimum of one character.
- It should have no white house.
- It must be adopted by a dot(.).
- It ought to finish with any one of many file extensions:
ex : jpg, txt, mp3, png, ppt, .. and so on
Examples:
Enter: str = doc.txt
Output: textual content
Clarification: Right here the string extends .txt then it’s a textual content file.Enter: str = image.jpg
Output: image
Clarification: Right here the string extends .jpg then it’s a image file.
Under are the steps concerned within the implementation of the code:
- Get the String.
- Transformed the string to lowercase utilizing the decrease methodology, in case it’s in uppercase.
- Extract the extension of the substring ranging from the place of the ‘.’ character to the top of the string.
- If the extension matches one of many recognized extensions, the operate returns a string indicating the kind of the file (e.g. ‘picture’ for picture information, ‘audio’ for audio information, and so on.). If the extension doesn’t match any of the recognized extensions, the operate returns ‘unknown’.
- print the file identify.
Under is the implementation of the above method:
C++
// C++ code for the above method: #embrace <bits/stdc++.h> utilizing namespace std; string get_file_type(string filename) { // Convert the string to lowercase remodel(filename.start(), filename.finish(), filename.start(), ::tolower); // Get the substring after (.) string ext = filename.substr(filename.find_last_of(".")); if (ext == ".txt" || ext == ".csv" || ext == ".tsv") { return "textual content"; } else if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif") { return "picture"; } 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 "doc"; } else if (ext == ".xls" || ext == ".xlsx") { return "spreadsheet"; } else if (ext == ".ppt" || ext == ".pptx") { return "presentation"; } else if (ext == ".zip" || ext == ".rar" || ext == ".7z") { return "archive"; } else if (ext == ".exe" || ext == ".msi") { return "executable"; } else if (ext == ".java" || ext == ".c" || ext == ".py" || ext == ".cpp") { return "code"; } else { return "unknown"; } } // Driver code int predominant() { string str = "doc.txt"; // Operate name string ans = get_file_type(str); cout << ans << endl; return 1; }
Time Complexity: O(1)
Auxiliary House: O(1)
Associated Articles: