Harry Potter got a challenge to unlock the path and tell its filename and extension.
He uses "Alohomora" to unlock the file path and break the code just like Hermione used to unlock the door.
Help him to unlock the path and extract the file path, file name and its extension as shown below:
Example:
Input: s ="C:\Users\admin\Pictures\flower.jpg"
Output:
C:\Users\admin\Pictures\flower.jpg Path: C:\Users\admin\Pictures\ File name: flower Extension: jpg
Approach:
C++
#include <bits/stdc++.h>using namespace std;void harryFile(string s){int n = s.size();int i = n - 1;string ext = "";string file = "";while (i > 0){while (i > 0 && s[i] != '.'){ext += s[i];i--;}i--;int x = s[i];while (i > 0 && x != 92){file += s[i];i--;x = s[i];}break;}string path = "";for (int j = 0; j <= i; j++)path += s[j];cout << "Path: " << path << "\n";reverse(file.begin(), file.end());cout << "File name: " << file << "\n";reverse(ext.begin(), ext.end());cout << "Extension: " << ext << "\n";}int main(){string s;cin >> s;harryFile(s);return 0;}
No comments:
Post a Comment