CSC 161 Grinnell College Fall, 2011
Imperative Problem Solving and Data Structures

Laboratory Exercise on More Input and Output


The goal of this lab is to continue to practice the concepts of and functions concerning input and output with the C programming language.

Lab Exercises

Remember that for exercises using strings, you need to include the following lines in the head of your code:

#include <stdio.h>
#include <string.h>

Introduction to scanf

  1. After reviewing scanf-example.c, write a short program that asks for your name, stores your name using scanf, then prints the word "hello" and your name.

  2. Run your program again, except this time enter a number. Does the program still work? Why?

    Hint: what type does scanf assign input to in your code?

  3. Run your program, using a phrase as input (e.g. "down the hill"). What is the result?

    Recall that scanf assigns input to a variable; when assigning input to a string, a blank space is not considered to be part of a string by scanf. This is in contrast to getchar, which treats blank space characters the same as alpha-numeric or punctuation characters.

  4. Why might someone use getchar instead of scanf?

Comparing Strings

  1. Some programs perform different actions based on the entered information. For instance, programs that change a password often require the user to enter the new password twice to guard against typos. If the input is not the same for both entries, the password is not changed. Write a short program that prompts the user to enter a word, then prompts the user to retype the word. If the input matches, the program should print out the line "The entered word was (word)" (parentheses not included). If the input does not match, the program should print out a line which includes both entries.

    Hint: the <string.h> function strcmp (char * str1, char * str2); compares two strings, and

    • returns a negative number if str1 comes before str2 in dictionary order (upper/lower case matters)
      Examples: strcmp("cat", "dog") < 0
      strcmp("that", "this") < 0
    • returns 0 if the strings match.
      Examples: strcmp("cat", "cat") == 0
      but strcmp("cat", "cAt") ≠ 0 (lower case a and upper case A are different)
    • returns a positive number if str1 comes after str2 in dictionary order (upper/lower case matters)
      Examples: strcmp("dog", "cat") > 0
      strcmp("this", "that") > 0
    1. Test whether the program works as intended by entering words which match, and words which do not match.

    2. Enter words which are identical, except for capitalization (for example, "apple" versus "apPle"). What happens? Why do you get this result?

    3. Modify your program so that it makes all the letters in a word the same case, and rerun your tests.

    Real World Note: The above program is a simplified version of a normal password-checking function used to safeguard data. In real-world applications, a database stores the valid password(s) after the passwords have been "hashed". Hashing takes input, puts the input through an algorithm that returns the data in a way that is nearly impossible to extract the original data from, and stores it. Then, when a user enters their password in the program, their password as it is entered is hashed and compared with the stored hashed password. If the hash results are the same, the user may access the data protected by the password. One of the main reasons to store the hashed password instead of the plain text of the password is security; even if a hacker were to gain access to the password list, they would not know the actual passwords to access the secure data.

Converting Strings

  1. In exercise 2, you found out that entering a number which is assigned to a string yields the number, but treated as a string. For example, if you entered 123, the string result is "123". Write a program that reads in an integer as a char * variable, and converts the number to an integer.

    You may not use the function atof (char * str); in your program.

Robot I/O

  1. Write a short program that connects to the robot, asks for beep length and pitch, beeps for the assigned length and pitch, and disconnects from the robot.

  2. Modify the program you wrote in the previous exercise so the program continues, prompting for input and beeping, until the time entered is 0.

  3. Now modify the program you have written to count the number of beeps, and when the time entered is 0, print the number of beeps before exiting the program.

Reviewing I/O

  1. Write a short program that takes in a string and prints it out, using scanf and printf.
    1. Modify your program so that it uses putchar to print the string.
    2. Now modify the program to use getchar to take in the string.
    3. Give one reason a programmer may choose to use each version.

Reminder: Complete Evaluation Form

When you have finished this lab, be sure to fill out its evaluation form in the "Lab Evaluation" section for CSC 161 on Pioneer Web.