⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex4_12.cpp

📁 Wrox.Ivor.Hortons.Beginning.Visual.C.Plus.Plus.2008 With sourcecode
💻 CPP
字号:
//Ex4_12.cpp
// Searching a string
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
using std::strlen;
using std::strstr;

int main()
{ 
  char* str = "Smith, where Jones had had \"had had\" had had \"had\"."
                         "\n\"Had had\" had had the examiners' approval.";
  char* word = "had";
  cout << "The string to be searched is: "
       << endl << str << endl;

  int count = 0;                  // Number of occurrences of word in str
  char* pstr = str;               // Pointer to search start position
  char* found = 0;                // Pointer to occurrence of word in str            
  while(true)
  {
    found = strstr(pstr, word);
    if(!found)
      break;
    ++count;
    pstr = found+strlen(word);    // Set next search start as 1 past the word found
  }
  cout << "\"" << word << "\" was found " 
       << count << " times in the string." << endl;
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -