-
Write a C program called numberLines1.c. It should copy standard input to standard output
a line at a time and number the output lines. Do not use the readlines function from
the text; it is there to make certain things clear to the reader, not to replace existing C
library functions. Use
fgets
and fputs
(and anything else you need).
The output lines should be numbered like this:
0001 xxxxxxx
0002 xxxxxxx
...
0010 xxxxxxx
0011 xxxxxxx
...
0999 xxxxxxx
1000 xxxxxxx
...
You can assume that there are fewer than 10,000 lines in the input.
The line number should be followed by a single blank, then by the line itself as shown above.
For this exercise, do not use a large buffer for reading/writing. Use a buffer of size 20.
You can assume that no line is longer than 19 characters, including the newline character '\n' at the
end of the line.
-
Write a C program called numberLines2.c. Its task is the same as that for the
previous question, except that you cannot make any assumptions about the length of the lines
that are being read. Nevertheless, you should still use a buffer of size 20.
-
Write a C program called reverse.c. It should read standard input a (string) token at
a time. Each token it reads should be output alone on a line, but written backwards, as in
the following example (the dollar sign $ is the Bourne shell prompt).
$
$ cat someStrings
here
are
some
strings
$
$ reverse < someStrings
ereh
era
emos
sgnirts
$
$
The file reverse.c should contain a main
method and a separate
method void reverse(char string[])
. The reverse
function should not
use any auxillary data structures; for example, it should not try to do anything like copying
its string argument into an array and then copying the array backwards into the string.
For this exercise, assume that none of the strings read is longer than
29 characters (not counting the '\0' that will be appended by the scanf() function).
See the man page entry for the C-library strlen
function.
-
Write a C program called palindrome.c that reads from standard input
and writes to standard output. Your program should copy to
standard output all input tokens that are palindromes.
No other output should be produced.
You can assume that no input token is longer than 30 bytes.
Each token that is a palindrome should be output alone on a line, as in
the following example (the dollar sign $ is the Bourne shell prompt).
$ cat someStings
hello
ollo
I a
aaa
aye madam aye
eye madam eye
$ palindrome < someStings
ollo
I
a
aaa
madam
eye
madam
eye
$
-
Here's another simple I/O exercise. Write a C program called twoTokens.c that reads from
standard input a line
at a time. Assume no line is longer than 50 bytes. Copy to standard output all lines
that contain exactly 2 tokens. See the man page for the scanf family of functions and
look at sscanf. The trick here is to use fgets to read into a buffer and then use sscanf to read from the buffer. And recall that scanf() returns the number of conversions made - so try to read 3 tokens and see if it returns 2.
The following might be a sample run:
$ cat someLines
two tokens
but not here
and here
1 2
one two
one
one two three
easy exercise
$ twoTokens < someLines
two tokens
and here
1 2
one two
easy exercise
$