source: doc/theses/mike_brooks_MMath/word.cc@ bd72f517

Last change on this file since bd72f517 was 38e17b0, checked in by Peter A. Buhr <pabuhr@…>, 5 months ago

add string example to extract words from line

  • Property mode set to 100644
File size: 859 bytes
RevLine 
[38e17b0]1#include <string>
2#include <iostream>
3using namespace std;
4int main() {
5 string line, alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
6 size_t i;
7
8 for ( ; getline( cin, line ); ) {
9 for ( ;; ) { // scan words off line
10 string::size_type posn = line.find_first_of( alpha );// find position of 1st alphabetic character
11 if ( posn == string::npos ) break; // any characters left ?
12 line = line.substr( posn ); // remove leading whitespace
13 posn = line.find_first_not_of( alpha ); // find position of 1st non-alphabetic character
14 if ( posn != string::npos ) {
15 cout << line.substr( 0, posn ) << endl; // extract word from start of line
16 line = line.substr( posn ); // delete word from line
17 } else {
18 cout << line << endl; // extract word from start of line
19 line = "";
20 }
21 } // for
22 } // for
23}
Note: See TracBrowser for help on using the repository browser.