Laboratory Exercise on More Input and Output
Goals
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
After reviewing
scanf-example.c, write a short program that asks for your name, stores your name usingscanf, 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
scanfassign input to in your code?- Run your program, using a phrase as input (e.g. "down the hill"). What
is the result?
Recall that
scanfassigns input to a variable; when assigning input to a string, a blank space is not considered to be part of a string byscanf. This is in contrast togetchar, which treats blank space characters the same as alpha-numeric or punctuation characters. - Why might someone use
getcharinstead ofscanf?
Comparing Strings
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>functionstrcmp (char * str1, char * str2);compares two strings, and-
returns a negative number if
str1comes beforestr2in 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
butstrcmp("cat", "cAt")≠ 0 (lower caseaand upper caseAare different) -
returns a positive number if
str1comes afterstr2in dictionary order (upper/lower case matters)
Examples:strcmp("dog", "cat")> 0
strcmp("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.
-
returns a negative number if
Converting Strings
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
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.
Reviewing I/O
- Write a short program that takes in a string and prints it out,
using
scanfandprintf.- Modify your program so that it uses
putcharto print the string. - Now modify the program to use
getcharto take in the string. - Give one reason a programmer may choose to use each version.
- Modify your program so that it uses
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.