Search a null-terminated string: determine if url and url2 contain .com, .net, or .org. : string search « string « C++ Tutorial






#include <iostream>
#include <cstring>

using namespace std;

int main() {

  const char *url = "server.com";
  const char *url2 = "Apache.org";
  const char *emailaddr = "Herb@server.com";

  const char *tld[] = { ".com", ".net", ".org" };

  const char *p;

  // determine if url and url2 contain .com, .net, or .org.
  for(int i=0; i < 3; i++) {
    p = strstr(url, tld[i]);
    if(p) cout << url << " has top-level domain " << tld[i] << endl;

    p = strstr(url2, tld[i]);
    if(p) cout << url2 << " has top-level domain " << tld[i] << endl;
  }

  return 0;
}








15.20.string search
15.20.1.Search a null-terminated string: determine if url and url2 contain .com, .net, or .org.
15.20.2.Search for a specific character
15.20.3.Search for any of a set of characters. In this case, find the first @ or period.
15.20.4.string search with custom comparison function