Cabbages Lab

OBJ: use the apstring class

  1. In this lab, you will read in a text file, echoing each word to the screen, one word per line, remembering the longest word. Print the current word count and a space before each word. After all the words have been read in, display the longest word with a message. For example, if the above paragraph were the entered text, the output would be:

    Words found in text --
    	1 In
    	2 this
    	3 lab,
    	    :
    	50 message.
    	The longest word in the text is .
    For your text file, use the example from class, "cabbages.txt". You may obtain a copy of it from your teacher. When you write your client program "cabbages.cpp", be sure to add the statement #include "apstring.h" and "apvector.h" at the top. Check that the compiler knows where to find the header file.

    Hint for Part 1:

    void readIn ( )
    {	
    	ifstream infile ("cabbages.txt");
    	if ( ! infile )
    	{	cout << "File could not be opened." << endl;
    		exit (1);
    	}
    	apstring temp;			// for processing before putting into an array
    	while ( infile >> temp )	// reads one contiguous stream of non-space
    	{				// characters- discards delimiters (space, eoln, eof, tab)
    		:
    		//  print to screen with running count
    		:
    	}
    
  2. Store all the words found in the text file, excluding punctuation, into an array (apvector) of strings, eliminating duplicates and converting all to lowercase. Sort this array alphabetically. Finally display this sorted array of words, preceded by the index of each word.

    	Words sorted alphabetically with duplicates removed --
    	0 a
    	1 after
    	2 all
    	3 been
    	     :
    
    Hint for Part 2:

    void readIn ( apvector & words) 
    {	
    		:
    	int num, index = 0;
    	apstring temp;			// for processing before putting into an array
    	while ( infile >> temp )	// reads one contiguous stream of non-space characters
    	{				// discards delimiters (tab, space, eoln, eof)
    			:
    		//Set up an empty string to concatenate into, one letter at a time
    		words[index] = "";  	
    			:
    		num = temp.length( );
    		for (int j = 0;j < num;j++)   	// process one character at a time
    		{
    				:
    			// place only "accepted" lowercase, alphabetic characters into the string
    			word[index] = word[index] + temp[ j ];
    				:
    			index++;		// increment number of "words"
    		}  // for
    	}  // while
    	words.resize(index);		// adjust size to actual length when finished reading
    }
    
  3. BONUS - grep (globally search for the regular expression and print the lines) is an utility program from UNIX that scans a file for a given string and prints all lines containing the string. Modify your program to "grep" a phrase and display the line number and each line containing it, highlighting the phrase. For example, using the opening paragraph , the call

    	grep("the longest word"); 
    
    would display

    	Line 2: remembering , print the current word count and a space before each word.  
    	Line 3: After all the words have been read in, display  with a message.
    
    If the phrase does not appear in any line, state that.

    Hint for Part 3: Use the overloaded getline function to read in a line, including white space, from either the text file or the keyboard

    	while ( getline(infile >> temp) ) { .. }
    	getline(cin, temp);
    


Continue to:  Unit 2  / Prev  / Next