Lab 4 Working with strings, arrays, and pointers

Objectives

Note: don't worry if you can't finish the entire lab exercise. Use turnin (see step 6) to turn in as much as you've completed before you leave the lab. Make sure you finish the rest of the lab on your own time.

What you should do...

  1. Sign the attendance sheet.

  2. Copy this partially-completed program into your directory, into a file named lab4.c:
    /* Lab 4
       Double all characters in a string
       YOUR NAME
       YOUR SECTION NUMBER
    */
    
    
    #include <stdio.h>
    
    /* HERE IS THE FUNCTION PROTOTYPE */
    void doubleAll (const char stringa[], char stringb[]);
    
    int main()
    {
      char instring[40];
      char outstring[80];
    
      printf ("Please enter a string:   ");
      fgets (instring, 40, stdin);
    
      /* PUT YOUR FUNCTION CALL HERE */
    
    
      printf ("with characters doubled: ");
      puts(outstring);
    
      return 0;
    }
    
    /* PUT YOUR FUNCTION DEFINITION HERE */
    /* REFER TO THE PROTOTYPE FOR THE FUNCTION HEADING */
    
    

  3. Complete the program by writing and calling a function which doubles all the characters in a string. Use array subscript notation rather than pointers when you write the body of the function. Call the function with instring and outstring as arguments. The new string, with twice the number of characters, will be placed in outstring.

  4. Compile and run the program. Here is a sample execution.
    
     Please enter a string:	are we having fun yet?
     With letters doubled:  aarree  wwee  hhaavviinngg  ffuunn  yyeett??
    
    
    If you are running out of time, skip ahead to step 6, and complete step 5 on your own time. If you have at least 15 minutes left, proceed to step 5.

  5. Now change your program so that it uses pointers instead of array subscripts. Change the prototype to:
    void doubleAll(const char *stringa, char *stringb);
    
    Change the function heading to match the prototype. Change the body of the function so that it does not use array subscripts to access the elements of the array, but uses pointers instead. Your function call will remain the same as before.

  6. Turn in your files using the turnin program. The Unix command you should use to submit your files is
    /cs/bin/turnin submit cs2301 LAB4 lab4.c 
    

See you in two weeks (Thanksgiving break starts next Wednesday)!