CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Laboratory 4 Solutions

    Solution to compare.cc code:

    /*******************************************************************
     * Name: Brenda Student
     * Date: 2/11/14
     * Course: CSCI 132 Section 01
     * Assignment: Lab 4
     * Instructor: Royden
     * Program: compare.cc
     * Purpose: Compare two strings separated by a colon and report if
     *          they are the same, the same length, or which is longer.
     ***************************************************************************/
    
    #include <iostream>
    #include "queue.h"
    
    using namespace std;
    
    int main(void) {
      char c, c1;          //variables for characters read in
      bool same = true;    //keep track of whether the strings are the same
      Queue firstString;   //Store characters in first string
      
      cout << "Please enter two strings separated by a colon: " << endl;
      cin.get(c);
      while ( (c != '\n') && (c != ':')) {
        if ( firstString.append(c) == overflow ) {
          cout << "Too many characters in first string. " << c << " lost." << endl;
        } //end if
        cin.get(c);
      } //end while
      
      if (c == '\n') {
        cout << "The input line has no colon. " << endl;
      } else {
        cin.get(c);
        while (( c != '\n' ) && (!firstString.empty())) {
          firstString.retrieve(c1);
          if (c != c1) {
    	    same = false;
          }
          firstString.serve();
          cin.get(c);
        } //end while
    
        if (!firstString.empty( )) {
          cout << "The lefthand string is longer than the righthand string." << endl;
        } else if ( c != '\n') {
          cout << "The righthand string is longer than the lefthand string." << endl;
        } else if (!same ) {
          cout << "The two strings are the same length, but they are different." 
    	   << endl;
        } else {
          cout << "The two strings are the same. " << endl;
        } //end inner else if
      } // end outer else
    } // end main
    
    
    

    Home | | Syllabus | | Assignments | | Lecture Notes