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.