Write a program to take input a URL(website address). The input has to be validated as per the following rules : -
The program should throw separate exception object with an error code and error message as data member, for each of the rule, and display the appropriate message. Include a catch all block. If no exception occurs, then the program displays the URL.
#include <iostream>
using namespace std;
bool s_contains(string s, char c)
{
if(s.find(c)<s.length()){
return true;
}
else {
return false;
}
}
int main()
{
string url;
cout<<"\nEnter a url: ";
cin>>url;
//The URL should start with -> www.
bool startWith=false;
if (url.find("www.") == 0)
{
startWith=true;
}
//Following characters should not be there : underscore( _ ), hyphen(-), equals (=), spaces.
bool underscore=s_contains(url,'_');
bool hyphen=s_contains(url,'-');
bool equals=s_contains(url,'=');
//Maximum length should be 2048 characters.
bool maxLength=false;
if (url.length()<=2048){
bool maxLength=true;
}
if(startWith==1 && underscore==0 && hyphen==0 &&equals==0 && maxLength==1){
cout<<"\nThe URL is valid";
}
else{
cout<<"\nThe URL is invalid";
}
return 0;
}
Comments
Leave a comment