| CSC 161 | Grinnell College | Spring, 2012 |
| Imperative Problem Solving and Data Structures | ||
The goal of this lab is to continue to practice the concepts of and functions concerning input and output with the C programming language.
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>
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.
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?
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.
getchar instead
of scanf?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
str1 comes
before str2 in dictionary order (upper/lower case
matters)
strcmp("cat", "dog") < 0strcmp("that", "this") < 0
strcmp("cat", "cat") == 0strcmp("cat", "cAt") ≠ 0 (lower case a
and upper case A are different)
str1 comes
after str2 in dictionary order (upper/lower case
matters)
strcmp("dog", "cat") > 0strcmp("this", "that") > 0
Test whether the program works as intended by entering words which match, and words which do not match.
Enter words which are identical, except for capitalization (for example, "apple" versus "apPle"). What happens? Why do you get this result?
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.
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 atoi (char * str); in your
program. atoi is a built-in function that takes in a string and returns it as an int.
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.
Modify the program you wrote in the previous exercise so the program continues, prompting for input and beeping, until the time entered is 0.
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.
scanf and printf.
putchar to print the
string. getchar to take in the
string.Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.