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

📄 newfind.cpp

📁 Think in C++ 第二版源码
💻 CPP
字号:
//: C17:NewFind.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

#include <string>

#include <iostream>

using namespace std;



// Make an uppercase copy of s:

string upperCase(string& s) {

  char* buf = new char[s.length()];

  s.copy(buf, s.length());

  for(int i = 0; i < s.length(); i++)

    buf[i] = toupper(buf[i]);

  string r(buf, s.length());

  delete buf;

  return r;

}



// Make a lowercase copy of s:

string lowerCase(string& s) {

  char* buf = new char[s.length()];

  s.copy(buf, s.length());

  for(int i = 0; i < s.length(); i++)

    buf[i] = tolower(buf[i]);

  string r(buf, s.length());

  delete buf;

  return r;

}



int main() {

  string chooseOne("Eenie, meenie, miney, mo");

  cout << chooseOne << endl;

  cout << upperCase(chooseOne) << endl;

  cout << lowerCase(chooseOne) << endl;

  // Case sensitive search

  int i = chooseOne.find("een");

  while(i != string::npos) {

    cout << i << endl;

    i++;

    i = chooseOne.find("een", i);

  }

  // Search lowercase:

  string lcase = lowerCase(chooseOne);

  cout << lcase << endl;

  i = lcase.find("een");

  while(i != lcase.npos) {

    cout << i << endl;

    i++;

    i = lcase.find("een", i);

  }

  // Search uppercase:

  string ucase = upperCase(chooseOne);

  cout << ucase << endl;

  i = ucase.find("EEN");

  while(i != ucase.npos) {

    cout << i << endl;

    i++;

    i = ucase.find("EEN", i);

  }

} ///:~

⌨️ 快捷键说明

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