Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #i

ID: 3678419 • Letter: #

Question

 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include "tbl.h"   // recommended utility function that "trims" leading and trailing //   whitespace from the string s. // // Examples:   // //              "   abc  " would be trimmed to "abc" //              "   abc 123  " would be trimmed to "abc 123" (embedded whitespace preserved). // //    // Note that, as specified, the modification is "in-place" -- no new string buffer //    is created or allocated.  This is functionally fine because the string can only //    get shorter. // // Purpose:  input files may be formatted for easy visual display and so various strings  //    (column labels and row names) may be "padded" with white space for alignment purposes. //    Such padding should not be considered part of a row name or column label. // // Comment:  the library function isspace should be handy.  (In principle, the input file //    could use different types of whitespace characters -- such as tabs; isspace figures //    this out for you. // // Comment:  this is actually kind of an interesting little programming exercise --  //    eliminating leading and trailing whitespace without an additional buffer (and  //    presumably in one-pass. //  static void trim_string(char *s) {   }   static char * str2row(Table *t, char *buf);   Table * tbl_init() { int r, c;    Table *t = malloc(sizeof(Table));    t->sort_by = 0; // set it to something   t->direction = 1; // default increasing    t->nrows = 0;   t->ncols = 0;    t->rows = malloc(MAX_ROWS*sizeof(Row));   for(r=0; r<MAX_ROWS; r++){         t->rows[r].rownum = r;         t->rows[r].vals = NULL;         t->rows[r].sort_by_ptr = &(t->sort_by);         t->rows[r].dir_ptr =                  &(t->direction);   }   return t; }  void tbl_free(Table *t) { int i;    for(i=0; i<t->nrows; i++)          free(t->rows[i].vals);   free(t->rows);   free(t); }  int whitespace(char *s) {    while(*s != '') {         if(!isspace(*s))           return 0;         s++;   }   return 1; }           Table * tbl_from_stream(FILE *stream) { int lno; Table *t; char buf[MAX_LINE_LEN+1]; char *res; int n;    t = tbl_init();   lno = 0;   while(fgets(buf, MAX_LINE_LEN, stream)!=NULL){     lno++;     n = strlen(buf);     if(buf[n-1] != ' ') {             // FIX ADDED 3/13/16 -- allows last line of input to not be terminated             //   by ' ' and not incorrectly flag it as being too long.             //   The test below confirms that if there is indeed no ' ' terminating             //   the input, that we are also at the end of the input.             // FWIW, text files edited with vim seem to always have a newline             //   at the end of the last line... perhaps not necessarily true              //   with other editors.             if(getc(stream) != EOF) {                     tbl_free(t);                                     fprintf(stderr, "line %i too long ", lno);                     fprintf(stderr, "line: %s ", buf);                     return NULL;              }     }     // skip blank lines and lines beginning with '#'     //    which are interpreted as comments      // FIX ADDED 3/13/16.  Now smarter about what qualifies     //   as a "blank line" that should be skipped.     // New rule is:  any string that contains only white      //   space (including the empty string).     if(!whitespace(buf) && buf[0] != '#') {             res = str2row(t, buf);             if(res != NULL) {                     fprintf(stderr, "line %i: %s ", lno, res);                     tbl_free(t);                     return NULL;             }     }   }   return t; }        static void tbl_set_rownums(Table *t) { int i;    for(i=0; i<t->nrows; i++)          t->rows[i].rownum = i; }  /** * static:  file scope */ static int cmp(const void *a, const void *b) { Row *ra = (Row *)a; Row *rb = (Row *)b; int col = *(ra->sort_by_ptr); int dir = *(ra->dir_ptr);    if(ra->vals[col] < rb->vals[col])         return -dir;   if(rb->vals[col] < ra->vals[col])         return dir;   // return 0;  // not nec stable   return ra->rownum - rb->rownum;  // ensures stable sort }    void tbl_print(Table *t, FILE *stream) { int i, j;    if(t==NULL) {         fprintf(stream, "# attempted to print non-existing table ");         return;   }   for(i=0; i<t->nrows; i++){     for(j=0; j<t->ncols; j++) {         fprintf(stream, "%10.2f", t->rows[i].vals[j]);     }     fprintf(stream, " ");   } }  void tbl_sort(Table *t, int col, int incr){    t->direction = incr ? 1 : -1;   t->sort_by = col;      qsort(t->rows, t->nrows, sizeof(Row), cmp);    tbl_set_rownums(t); }        /** * success:  returns NULL * * failure:  returns error message */ static char * str2row(Table *t, char *buf) { int r, j; int nchars; double x;    if(t->nrows == MAX_ROWS)         return "table full";   t->nrows++;    r = t->nrows-1;   j=0;    if(t->nrows == 1) {         t->rows[r].vals = malloc(MAX_COLS*sizeof(double));   }   else {         t->rows[r].vals = malloc(t->ncols*sizeof(double));   }    while( sscanf(buf, "%lf%n", &x, &nchars) == 1){     if(j==MAX_COLS || (r>0 && j==t->ncols))         return "line too long";     t->rows[r].vals[j] = x;     buf += nchars;     j++;   }   if(r==0)         t->ncols = j;   else if(j < t->ncols) // too few columns         return "line too short";   return NULL; }               static int NumCmps = 0;  
 /** TODO  *    * function: cmp_rows * * desc:  takes two rows throug pointers a and b and returns: * *        equal if the rows are identical in every column (dimension) *        a_dom_b  if row given by a dominates that given by b: *               they are not equal and *               in every column, row-a is no larger than row-b (in that *                       same column) *        b_dom_a  if b dominates a  *        incomparable if neither a dominates b or vice-versa (and *               they are not equal). * */ 
  ROW_CMP cmp_rows(Row *a, Row *b) {    NumCmps++;    // DO NOT DELETE THIS LINE!!!    /**   *  TODO   *  replace code below with your solution...   * fprintf(stderr, "cmp_rows not implemented... returning 'equal' ");   */   return equal;  // placeholder... }   
 /** TODO  *    * function: tbl_minima * * desc:  constructs a table containing only the non-dominated rows *       of the given table t -- also known as the "minima" * *       The new table is returned. * *       A row is non-dominated if there is no other row that dominates it */ 
  Table * tbl_minima(Table *t) {    NumCmps = 0;   // DO NOT DELETE THIS LINE!!!    /**   *  TODO   *  replace code below with your solution...   */   fprintf(stderr, "tbl_minima not implemented... returning NULL ");   return NULL; }  // DO NOT CHANGE THIS FUNCTION!! int tbl_num_comparisons() {    return NumCmps;  } 
 typedef struct {   int rownum;   double *vals;   int *sort_by_ptr;   int *dir_ptr; } Row;  typedef struct {   int nrows;    int ncols;   int sort_by;   // column to sort by   int direction; // +1 for increasing;                   // -1 for decreasing   Row *rows; } Table; 

Explanation / Answer

// An efficient C++ program to remove all spaces

// from a string

#include <iostream>

using namespace std;

// Function to remove all spaces from a given string

void removeSpaces(char *str)

{

    // To keep track of non-space character count

    int count = 0;

    // Traverse the given string. If current character

    // is not space, then place it at index 'count++'

    for (int i = 0; str[i]; i++)

        if (str[i] != ' ')

            str[count++] = str[i]; // here count is

                                   // incremented

    str[count] = '';

}

// Driver program to test above function

int main()

{

    char str[] = "g eeks   for ge eeks ";

    removeSpaces(str);

    cout << str;

    return 0;

}

Run on IDE

// An efficient C++ program to remove all spaces

// from a string

#include <iostream>

using namespace std;

// Function to remove all spaces from a given string

void removeSpaces(char *str)

{

    // To keep track of non-space character count

    int count = 0;

    // Traverse the given string. If current character

    // is not space, then place it at index 'count++'

    for (int i = 0; str[i]; i++)

        if (str[i] != ' ')

            str[count++] = str[i]; // here count is

                                   // incremented

    str[count] = '';

}

// Driver program to test above function

int main()

{

    char str[] = "g eeks   for ge eeks ";

    removeSpaces(str);

    cout << str;

    return 0;

}