Answer to Question #268096 in C++ for Jack

Question #268096

Write a program to take input a URL(website address). The input has to be validated as per the following rules : -

  • The URL should start with -> www.
  • Following characters should not be there : underscore( _ ), hyphen(-), equals (=), spaces.
  • Maximum length should be 2048 characters.

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.


1
Expert's answer
2021-11-18T15:20:15-0500


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS