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

📄 prog6_09.cpp

📁 一本语言类编程书籍
💻 CPP
字号:
// Program 6.9 Searching a string
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

int main() {
  // The string to be searched
  string text = "Smith, where Jones had had \"had had\", had had \"had\"."
                "\n \"Had had\" had had the examiners' approval.";

  string word = "had";                // Substring to be found

  cout << endl  << "The string is: " << endl << text << endl;

  // Count the number of occurrences of word in text
  int count = 0;                      // Count of substring occurrences

  for(size_t index = 0 ; (index = text.find(word, index)) != string::npos ;
                                                             index += word.length(), count++)
    ;

  cout << "Your text contained "
       << count << " occurrences of \""
       << word  << "\"."
       << endl;

  return 0;
}

⌨️ 快捷键说明

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