1

I'm creating a simple program that is going to visit a website of the users choosing so I'm using an if statement like:

If (url == "http://")
{
    cout << ("Connecting to ") << url;
}
 else
 {
     cout << ("Invalid URL");
}

And I'm wondering how I can filter out strings that doesn't start with "http://" or "https://", I'm just starting out so help would be appreciated.

Rama
  • 3,222
  • 2
  • 11
  • 26

2 Answers2

1

A clear, but not particularly fast way, is to use (assuming url is a std::string)

if (url.substr(0, 7) != "http://" && url.substr(0, 8) != "https://"){
    /*I don't start with http:// or https:// */
}

Here I'm using substr to extract the start of a std::string then using the overloaded != operator.

Note that if url is shorter than 7 or 8 characters, the behaviour is still well-defined.

You could define static const char HTTP[] = "http://" and use sizeof(HTTP) - 1 &c. so you don't hardcode the lengths, but that might be going a step too far.

For more generality you could venture into the murky world of regular expressions. See std::regex.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

A possible option would be to store the known starting protocols into a vector of strings then use that vector and its fuctions as well as the strings functions to do your tests and if your url is a string object comparison is easy.

#include <string>
#include <vector>

int main {
    const std::vector<std::string> urlLookUps { "http://", "https://" };

    std::string url( "https://www.home.com" );

    unsigned int size1 = urlLookUps[0].size();
    unsigned int size2 = urlLookUps[1].size();

    if ( url.compare( 0, size1, urlLookUps[0] ) == 0 ||
         url.compare( 0, size2, urlLookUps[1] ) == 0 ) {
         std::cout << url << std::endl;
    } else {
         std::cout << "Invalid Address" << std::endl;
    }                  

    return 0;
}

EDIT

You can take this to the next step and turn it into a simple function

#include <string>
#include <vector>

void testUrls( const std::string& url, const std::vector<std::string>& urlLookUps ) {

    std::vector<unsigned int> sizes;

    for ( unsigned int idx = 0; idx < urlLookUps.size(); ++idx ) {
        sizes.push_back( urlLookUps[idx].size() );
    }

    bool foundIt = false;
    for ( unsigned int idx = 0; idx < urlLookUps.size(); ++idx ) {
        if ( url.compare( 0, sizes[idx], urlLookUps[idx] ) == 0 ) {
            foundIt = true;
            break;
        } 
    }

    if ( foundIt ) {
        std::cout << url << std::endl;
    } else {
        std::cout << "Invalid URL" << std::endl;
    }

} // testUrls

int main() {
    const std::vector<std::string> urlLookUps { "http://", "https://" };
    std::string url1( "http://www.home.com" );
    std::string url2( "https://www.home.com" );
    std::string url3( "htt://www.home.com" );

    testUrl( url1, urlLookUps );
    testUrl( url2, urlLookUps );
    testUrl( url3, urlLookUps );

    return 0;
} // main

This way you can pass both the URL to the function as well as a container of url protocols that the user may want to populate themselves. This way the function will search through all the strings that are saved into the vector of strings.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59