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

📄 pr0823.cpp

📁 practice c++, it is from the book http://www.amazon.com/Schaums-Outline-Programming-John-Hubbard
💻 CPP
字号:
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Problem 8.23 on page 203
//  Counting the words in a line of text

#include <iostream>
using namespace std;

int numWords(const char* s);

int main()
{ char *line = new char[81] ;
  cout << "Enter line of text below:" << endl ;
  cin.getline( line, 80 ) ;
  int count = numWords( line ) ;
  cout << "numWords(\"" << line << "\") = " << count << endl ;
  return 0 ;  
}

int numWords( const char* s)
{  if ( s == NULL ) return 0 ;
   int wordCount = 0 ;
   char * Copy = new char[ strlen(s) ] ;
   Copy = strcpy( Copy, s ) ;
   char * p = strtok( Copy, "\n \v\t\f\r" ) ;
   while ( p )    
   {  char ch0 = p[0];  // check whether first char is letter
      if ( ((ch0 >= 'a') && (ch0 <= 'z') ) ||   // lowercase
           ((ch0 >= 'A') && (ch0 <= 'Z') ) )    // uppercase
            wordCount++ ; 
      p = strtok( NULL, "\n \v\t\f\r" ) ;
   }    // end while (p)
   return wordCount ;
}

⌨️ 快捷键说明

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