-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ27.cpp
More file actions
66 lines (46 loc) · 1.4 KB
/
Q27.cpp
File metadata and controls
66 lines (46 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 27. Write a program to extract the file extension from a given filename.Example: Input: "document.pdf", Output: "pdf".
#include <iostream>
#include <string>
using namespace std;
string getFileExtensionBruteForce(string filename) {
int pos = filename.rfind('.');
if (pos != string::npos && pos != 0) {
return filename.substr(pos + 1);
}
return "";
}
int main() {
string filename = "document.pdf";
cout << "File Extension (Brute Force): " << getFileExtensionBruteForce(filename) << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
string getFileExtensionEasy(string filename) {
size_t pos = filename.find_last_of('.');
if (pos != string::npos && pos != 0) {
return filename.substr(pos + 1);
}
return "";
}
int main() {
string filename = "document.pdf";
cout << "File Extension (Easy Approach): " << getFileExtensionEasy(filename) << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
string getFileExtensionOptimal(string filename) {
if (filename.empty() || filename.find('.') == string::npos) {
return "";
}
size_t pos = filename.find_last_of('.');
return filename.substr(pos + 1);
}
int main() {
string filename = "document.pdf";
cout << "File Extension (Optimal Approach): " << getFileExtensionOptimal(filename) << endl;
return 0;
}