Use strchr(), strpbrk(), and strstr() to search a null-terminated string. - C++ Data Type

C++ examples for Data Type:char array

Description

Use strchr(), strpbrk(), and strstr() to search a null-terminated string.

Demo Code

#include <iostream>
#include <cstring>
using namespace std;
int main() {/*from   ww w . ja v  a 2s. co  m*/
   const char *url = "abc";
   const char *url2 = "def";
   const char *emailaddr = "this is a test abc def .com .net .org";
   const char *tld[] = { ".com", ".net", ".org" };
   const char *p;
   // First, 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;
      }
      // Search for a specific character.
      p = strchr(emailaddr, '@');
      if(p)
         cout << "Site name of e-mail address is: " << p+1 << endl;
      p = strpbrk(emailaddr, "@.");
      if(p)
         cout << "Found " << *p << endl;
      return 0;
}

Result


Related Tutorials