Files for this lab are located in: /stage/classes/archive/2011/summer/50101-1/lab/lab3/src
/* Example use of scanf */ char c; printf("Enter a letter: "); scanf("%c",&c);In this lab you will use the function getchar(), which reads a single character from standard input.
First, look at the program in file ex1.c:
#include <stdio.h> int main(void) { char ch; printf("Enter your input: "); while ((ch=getchar()) != 'C') { putchar(ch); } return 0; }This program uses the getchar() function to read individual characters from standard input. We say that standard input is a stream, meaning that the program sees standard input as a sequence of characters. The program also uses the function putchar() to write a single character to standard output, which is the stream of characters that you see at the terminal when you run your program.
Next, compile this program. Then run it from the command line, and enter this sentence as input. (Hit Enter at the end of the line.):
In 1983, a committee was formed to standardize the C programming language.
You should see this output:
$ ./ex1 Enter your input: In 1983, a committee was formed to standardize the C programming language. In 1983, a committee was formed to standardize the $Here in the lab script, your input is shown in italics to distinguish it from program output. (Of course it does not appear that way in the terminal.)
You can see that this program echos the user input up to the character 'C' and then stops. This is because we have indicated in the while-loop condition that the loop should exit when an upper-case 'C' is read from standard input. The program does not output a newline before it terminates, so the shell prompt (shown above as '$') appears on the same line as the output.
The syntax in the while-loop may be new to you. Here, the function getchar() is called within the condition of the while-loop:
while ((ch=getchar()) != 'C')So the expression (ch = getchar()) is both an operand to != and an assignment statement. First, the value returned by getchar() is assigned to variable ch with operator =. Then, the expression on the left side of operator != evaluates to the value that is assigned to ch, and that value is compared to the character literal 'C'. The test will fail (and the while-loop will exit) when getchar() returns 'C'.
Run this program again, using the same sentence as input but with a newline in the middle:
In 1983,
a committee was formed to standardize the C programming language.
You should see this at your terminal (Your input is again shown in italics.):
Enter your input: In 1983, In 1983, a committee was formed to standardize the C programming language. a committee was formed to standardize the $Note that the input is repeated after every newline, not after every character. This is because your individual keystrokes are read into a buffer, and the program does not read from this buffer until you hit Enter. This is why individual characters are not echoed as soon as they are entered, even though putchar() is called right after getchar() in the while-loop.
You can read more about buffers in Ch. 8 in Prata.
First, create a file that contains the above sentence ("In 1983, ..." etc).
Next, redirect the contents to your ex1 program using '<' as shown below:
You can also redirect output using '>'. So the output of the program will be written to a file, and you will not see this output at the terminal.
You can then view the contents of your file with the Linux command cat:
The solution is that we use the value EOF to check for the end of the file. EOF is a special value (typically -1) that is not equivalent to any ASCII character.
Modify your while-loop so that it terminates when EOF is found, rather than the literal character value 'C'. You will need to change the condition of the while loop so that the return value of getchar() is compared to EOF instead of 'C':
You'll see that your program is now very similar to display 8.2 in the Prata text.
Now rerun your program using redirection:
You can simulate EOF at the terminal by hitting CTRL-D at the start of a new line. Try this now. After the period at the end of the sentence, hit return and then CTRL-D:
Redirection
With Linux, you can also redirect data from a file to your program, rather than typing your input at the terminal.
$ ./ex1 < myinput.txt
Enter your input: In 1983, a committee was formed to standardize the $
Note that only the output of the program is shown, including the prompt "Enter your input: ".
$ ./ex1 < myinput.txt > myoutput.txt
(But be careful when redirecting output with '>'. If myoutput.txt already exists then it will be overwritten without warning.)
$ cat myoutput.txt
Enter your input: In 1983, a committee was formed to standardize the $
Ch. 8 in Prata also covers redirection.
Using EOF (end-of-file)
Our program still has a somewhat awkward feature that it stops reading characters once it finds the letter 'C'.
So when should the program stop reading characters? If we stop at '\n', then our program will only see the first line of every file or input stream, but we may want to write programs that process multiple lines of text.
while ((ch = getchar()) != EOF)
$ ./ex1 < myinput.txt > myoutput.txt
$ cat myoutput.txt
Enter your input: In 1983, a committee was formed to standardize the C programming language.
$
$ ./ex1
Enter your input: In 1983, a committee was formed to standardize the C programming language.
In 1983, a committee was formed to standardize the C programming language.
$
Exercise 1
Modify ex1.c so that each word is printed on its own line.
Assume words are separated by spaces. You can also assume that there will be exactly one space between words.
Your output should look like this:
$ ./ex1 < myinput.txt
Enter your sentence: In
1983,
a
committee
was
formed
to
standardize
the
C
programming
language.
The first character in each word should start each line in the output, i.e. this is incorrect:
In
1983,
a
...
Your output should look like this:
$ ./ex2 Enter your input: AAA is headquartered in Aurora, Illinois, which is not too far from Chicago. A is headquartered in Aurora, Ilinois, which is not to far from Chicago.Remember to make changes in a new file, ex2.c.
The program ascii_demo.c is a simple program that outputs upper-case characters and their ASCII values.
#includeIn the printf() statement, character variable ch is displayed both as a character ("%c") and integer ("%d") value. Compile and run this program to observe the output.int main(void) { char ch='A'; while (ch <= 'Z') { printf("%c\t%d\n",ch,ch); ++ch; } return 0; }
What is the ASCII value of character 'Z'?
Next, modify this program so that it displays the ascii values of lower-case letters. You do not have to submit your work. Your output should look like this:
a 97 b 98 c 99 ... x 120 y 121 z 122What are the ASCII values of lower-case letters 'a' and 'z'? In general, what is the difference in ASCII values of an upper-case character and its lower-case equivalent?
Also note that this program increments a character variable:
++ch;Since character data types are stored as integer values, we can use them with addition, subtraction, and relational operators such as < and >.
Modify your solution for Exercise 1 (in a separate file, ex3.c) so that all characters are shown in upper case.
To do this, first add a function to your program called to_up(). This function should take one argument, a character, and return a character. If the input is a lower-case letter then to_up() will return the upper-case equivalent. Otherwise, to_up() will return the same character.
After you have implemented to_up(), modify your Exercise 1 solution so that only upper-case characters are shown.
The string.h library already contains a function called toupper() but you should write your own version for this lab. (So, don't call toupper() in your solution!)
Your output should look like this:
$ ./ex3 < myinput.txt Enter your input: IN 1983, A COMMITTEE WAS FORMED TO STANDARDIZE THE C PROGRAMMING LANGUAGE.You are given four files to test your output:
$ ./ex3 < in1.txt > myout1.txt $ diff myout1.txt out1.txt $Note that diff shows no output when two files are identical, as is the case in the above example. When two files are not identical, diff displays the differing lines. For example, here diff indicates that myout1.txt and out1.txt are not identical (myout1.txt is missing a comma).
$ diff myout1.txt out1.txt 3c3 < OATS --- > OATS,Update: You can also use the "-b" flag with diff, which ignores differences in white space:
$ diff -b myout1.txt out1.txt
When using hwsubmit:
Submit to cspp50101lab, NOT cspp50101.
First, be sure that every source file you submit includes this information in comments at the top of the file:
/* Sonjia Waxmonsky * wax * * ex2.c: Converts from USD to JPY * * Uses exchange rates from June 22, 2011 * */
Also include a simple README file in your lab3 directory. This file should include your first and last name, your username, the list of files that you are submitting, and any other comments or questions for the Lab TA. For example:
Sonjia Waxmonsky wax ex1.c ex2.c ex3.c
Do not submit any compiled binary files
You will submit this lab with the hwsubmit program.
$ hwsubmit cspp50101lab <path to your lab3 directory>Submit to cspp50101lab, NOT cspp50101.
prepared by Sonjia Waxmonsky