CSC 161 Grinnell College Spring, 2015
 
Imperative Problem Solving and Data Structures
 
 

Laboratory Exercise on Character-by-Character I/O

Goals

This lab provides practice with character-by-character input and output within the C programming language.

Preparation before Class

Work Started in Class

Reading Individual Characters

  1. Program get-3-char.c reads three characters from the keyboard and prints them.

    1. Copy get-3-char.c to your account, and compile it, and descibe briefly what it does.

    2. Run the program, entering abc on the same line (no spaces), and describe what happens.

    3. Run the program, entering a b c on the same line (with spaces between the letters), and describe what happens.

    4. Run the program, trying to enter a on one line, b on a second line, and c on a third line. What happens? In the output, the first line appears as $a$, but the second and third lines contain a single dollar sign. Why?

    5. Run the program, trying to enter ab on one line, c on a second line. Again, explain what happens and why.

    6. In the program, replace

        ch1 = getchar();
        ch2 = getchar();
        ch3 = getchar();
      

      by

      scanf ("%c", &ch1);
      scanf ("%c", &ch2);
      scanf ("%c", &ch3);
      

      Does the program behave the same, or is something different? Why or why not?

    7. In the previous step, replace the three scanf statements by the single statement:

      scanf ("%c%c%c", &ch1, &ch2, &ch3);
      

      Again, determine if the program behaves as before or does something else, and explain what you observe.

Reading One Line, Character by Character with getchar

  1. Program getchar-example.c reads and prints a line of characters from the keyboard. As you observed in Step 1, the single-character printing function putchar, like getchar, deals with a single character per call.

    1. Copy getchar-example.c to your account, compile it, and descibe briefly what it does.

    2. What happens when you enter more than one letter? Why?

    3. How about if you do not enter a letter, but simply press enter?

      Recall that getchar gets a single character; blank space and newline characters are considered viable characters.

    4. Why do you think the line

            putchar ('\n');
          

      is included?

      Like getchar, putchar takes a single character. The function putchar prints a single character to stdout.

  2. Although reading and printing one character at a time is sometimes useful by itself, a more common approach is to collect a line of characters in an array. This is illustrated in the getchar-line-example.c program.

    1. Copy getchar-line-example.c to your account, compile it, and descibe briefly what it does.

    2. Examine the first loop. Why does putchar(a) precede a = getchar()? Why does putchar('\n') appear after the loop? What happens if you move putchar('\n') inside the loop?

    3. Examine the second loop. What is the purpose of the i variable? The loop condition involves both an assignment and a comparison. Explain how that works and why it is used. Why is the statement line[i]=0 placed after the loop?

    4. What happens in the output if the line line[i]=0 is deleted from after the second loop? Explain.

  3. Write a program that does the following:

    • Reads a number from the keyboard (likely using scanf).
    • Clears any whitespace after the number from the first line of input, perhaps using the line
        while (getchar() != '\n');
      
    • declares an array one larger than the size typed by the user on the first line
    • reads the specified number of characters from the keyboard
    • adds a null character at the end of the array
    • prints the resulting string.
  4. Modify the characters as they are entered in the previous Step, so that it sets each letter to the opposite case when placing it in the string. Note that <ctype.h> has functions that compare types and modify letter cases.

    Examples:

    • apple -> APPLE
    • AlPhAbEt -> aLpHaBeT
    • X-Ray -> x-rAY

Homework

Programming with getchar

  1. Write a program that prints a prompt for input and analyzes a line entered by the user.

    1. Initially, the program should print the number of characters entered.

    2. Modify your program to also print the number of uppercase and lowercase letters in your input.

      Hint: You might use the <ctype.h> library.

    3. Modify your progrram further to print the number of whitespace characters (e.g., tabs and spaces).

Practice with printf

  1. As you have learned previously, it is important to pay close attention to variable types, and you may have seen errors that occur due to a type mismatch in a function. Look at the following code, where each printf statement uses the wrong type for the declared variable.

    #include <stdio.h>
    
    int
    main()
    {
      int x = 9, y;
      double s = 13.86;
      char * word = "computer";
      char ch = word[5];
      
      y = (int) s;
    
      printf ("\tThe value of x is %lf.\n", x);
      printf ("\tThe value of y is %f.\n", y);
      printf ("\tThe value of s is %d.\n", s);
      printf ("\tThe value of word is %c.\n", word);
      printf ("\tThe value of ch is %s.\n", ch);
      printf ("\tThe value of ch is %d.\n", ch);
    
      return 0;
    }
    
    1. Will the code compile? Save the program to a file in your directory and check. Run the program. Do any line(s) cause the program to terminate with a segmentation fault error? Why? Think about what each of the types are and how to print those types.

    2. Fix the line(s) that prevents the code from completing, and run the code. Explain why you get each incorrect value. How are integers, doubles, and floats treated by each other? How is a character different from a string?

      Hint: consider how each variable type is stored and evaluated.

    3. Change the printf statements so the correct values are printed when the program is run, and check that your changes fixed the code.

    4. Look at the following two lines of code:

              printf ("\tThe value of s is %lf.\n", s);
              printf ("\tThe value of s is %f.\n", s);
            

      Will these two lines print the same or different output? Why?

    5. Make sure both lines are in your code, compile, and run. Did the output match your expectations? If not, why did the code behave this way?

  2. Look closely at the following code. Will this program run as the programmer intended?

    /****
     *
     * Program to print the values of different types, with proper spacing.
     * It should print a as "7", b as "15.2594", and c as "something".
     *
     ****/
    
    #include <stdio.h>
    
    int
    main()
    {
      int a = 7;
      double b = 15.2594;
      char * c = "something";
    
      printf ("The value of a is %d.\n", a);
      printf ("The value of b is %.4lf.\n", b);
      printf ("The value of c is %9s.\n", c);
    
      return 0;
    }
    
    1. Compile and run the code. Is the output what you expected?

    2. In the statement that prints the value of b, edit the %.4lf to be %.2lf. What do you think the output will be? Compile and run the code to check.

    3. Now edit the statement that prints the value of b to %.6lf. What do you think will happen? Why? Run the code to check.

    4. Given your previous experience, what do you think will happen if you edit the line for the value of c from %9s to %6s. What about editing it to %12s? Run the program. Did your results match your expectations?

Practice with printf Spacing

As you may recall, when a number is placed between the '%' character and the variable type, the printf function allocates at least that many spaces for the information. If the information takes that much or more space, then the data are printed normally. However, if the information takes less space, the information is "right-aligned" with the allocated space, with the "extra" spaces left blank. In contrast, when a '.' character (followed by a number) is placed between the '%' and the variable type, only that much space is allocated after the decimal point. For example, if the number is 48.3557, and the printf statement indicates that only one value after the decimal should be printed, so the printed result will be rounded to one decimal place. Behind the scenes, the variable still stores the original value 48.3557, but the value printed is 48.4.

  1. Modify your program from Steps 6abc so that the code prints both the number and the percentage of uppercase letters, lowercase letters, and whitespace characters. Within the format string for the printf statement, print all percentages to exactly two decimal places.

Feedback Welcome

Development of laboratory exercises is an iteractive process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.