O(N) approach:
#include<iostream> #include <string> using namespace std; string domainName(string url) { string domain = ""; bool flag = true; for(int i = 0; i < url.size(); i++) { if(flag) { if(url[i] == '/') { flag = false; i+=2; if(url[i] == 'w') i = i+3; else i--; } continue; } else if(url[i] == '.') return domain; domain += url[i]; } return domain; } int main() { string url; cin >> url; cout << domainName(url) << endl; return 0; }
Naive approach:
#include<iostream> #include <string> using namespace std; string domainName(string url) { int x = url.find("www"); if(x==string::npos) { x = url.find("//"); x+=2; } else x+=4; return url.substr(x,url.find(".com")-x); } int main() { string url; cin >> url; cout << domainName(url) << endl; return 0; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
O(N) approach:
Naive approach: