Skip to content. Skip to navigation

ICTP Portal

Sections
You are here: Home Manuals on-line PGI Compiler pgC_lib stdlibug 12.3 An Example Function - Split a Line into Words
Personal tools
Document Actions

12.3 An Example Function - Split a Line into Words



Click on the banner to return to the user guide home page.

12.3 An Example Function - Split a Line into Words

Obtaining the Sample Program

In this section we will illustrate the use of some of the string functions by defining a function to split a line of text into individual words. We have already made use of this function in the concordance example program in Section 9.3.3.

There are three arguments to the function. The first two are strings, describing the line of text and the separators to be used to differentiate words, respectively. The third argument is a list of strings, used to return the individual words in the line.

void split 
   (string & text, string & separators, list<string> & words)
{
   int n = text.length();
   int start, stop;

   start = text.find_first_not_of(separators);
   while ((start >= 0) && (start < n)) {
      stop = text.find_first_of(separators, start);
      if ((stop < 0) || (stop > n)) stop = n;
      words.push_back(text.substr(start, stop - start));
      start = text.find_first_not_of(separators, stop+1);
      }
}

The program begins by finding the first character that is not a separator. The loop then looks for the next following character that is a separator, or uses the end of the string if no such value is found. The difference between these two is then a word, and is copied out of the text using a substring operation and inserted into the list of words. A search is then made to discover the start of the next word, and the loop continues. When the index value exceeds the limits of the string, execution stops.



©Copyright 1996, Rogue Wave Software, Inc.

Weather
No information available
 

Powered by Plone This site conforms to the following standards: