CS 2213/1, Spring 2005      
Quiz 4 Questions
Last Name:                            First Name:                  

Answers in red.

  1. Strings:

    Write a function with prototype void print_string(char[] ); that prints its string parameter one character at a time with a blank after each character. The function should not contain any square brackets ([ or ]). For example, the call print_string("Toad"); should print T o a d .

     

     

     


  2. Arrays of Strings: Consider the following program, with one function definition missing:

    By any method write a function print_strings that prints either array of strings above, in each case without the initial string "none", and with a comma and a blank after each string except the last one. print_strings should print a newline at the end. Thus the program above, after you add the function, should print:

    #include <stdio.h>
    void print_strings(char **);
    int main() {
       char *m[] = {"none","Jan","Feb","Mar","Apr","May","Jun",
                    "Jul","Aug","Sep","Oct","Nov","Dec",NULL};
       char *w[] = {"none","Mon","Tue","Wed","Thu","Fri","Sat","Sun",NULL};
       print_strings(m);
       print_strings(w);
    }
    
    void print_strings(char *r[]) {
       char **p = r+1;
       while(*p) {  /* or *p != NULL  */
          printf("%s", *p);
          if (*++p)   /* or *++p != NULL  */
             printf(", ");
       }
       printf("\n");
    }
    
    void print_strings(char **r) {
       char **p = &r[1];
       printf("%s", *p++);
       while(*p != NULL) {
          printf(", ");
          printf("%s", *p++);
       }
       printf("\n");
    }
    
    /* solution by S. Lecollier (corrected) */
    void print_strings(char **a) {
       int i, j;
       for (i = 1; *(a+i) != NULL; i++) {
          for (j = 0; *(*(a+i)+j) != '\0'; j++)
             printf("%c", *(*(a+i)+j));
          *(a+i+1) == NULL ? 
             printf("\n") : 
                printf(", ");
       }
    }
    
    /* adapted from J. Hammond */
    void print_strings(char **a) {
       while(*++a != NULL) {
          char *b = *a;
          while (*b != '\0')
             printf("%c", *b++);
          if (*(a+1) != NULL)
             printf(", ");
       }
       printf("\n");
    }
    
    /* adapted from J. Escobar */
    void print_strings(char **s) {
       int i = 0;
       while(*(s+i) != NULL) {
          if (strcmp(*(s+i), "none") == 0) i++;
          else {
             printf("%s", *(s+i));
             i++;
             if (*(s+i) != NULL)
                printf(", ");
          }
       }
       printf("\n");
    }
    
    /* solution by J. Packer */
    void print_strings(char **s) {
       int i = 1;
       printf("%s", *(s + i++));
       while(*(s + i) != NULL)
          printf(", %s", *(s + i++));
       printf("\n");
    }
    
    void print_strings(char *s[]) {
       int i = 1;
       while (s[i]) {
          printf("%s", s[i]);
          if (s[++i]) printf(", ");
          else printf("\n");
       }
    }
    

    This same function takes a simple form in Java, since an array knows how long it is, so there is no need for a NULL at the end.

    public class Quiz4 {
    
       public static void main(String[] args) {
          String[] m = {"none","Jan","Feb","Mar","Apr","May","Jun",
                    "Jul","Aug","Sep","Oct","Nov","Dec"};
          String[] w = {"none","Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
          printString(m);
          printString(w);
       }
    
       public static void printString(String[] a) {
          for (int i = 1; i < a.length; i++) {
             System.out.print(a[i]);
             if (i == a.length - 1)
                System.out.println();
             else
                System.out.print(", ");
          }
       }
    }
    


  3. 2-dimensional arrays:

    Consider the following 2-dimensional array:

    The array element at row 2 and column 4 can be referenced by the notation a[2][4]. Give another way to reference this array element that uses no square brackets. (This is the normal notation for an array of pointers to arrays, but the notation works here also.)


    Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.