-
The following picture shows a chunk of memory along with
five
char* variables, which point to various
locations in that memory.
a. What would printf("%s",p) print?
b. What would printf("%s",q) print?
c. What would printf("%s",r) print?
d. What would printf("%s",s) print?
e. What would printf("%s",t) print?
-
The following code replaces any non-printable characters in a
file with a "." character. The program is supposed to
function such
that with no command-line arguments
it reads from
stdin, while with a command-line argument it
takes that argument as the name of a file from whence it takes
its input. Add one line (where indicated) to make the code
so that it actually works this way.
FILE *fin;
if (argc > 1)
{
fin = fopen(argv[1],"r");
}
else
{
Add here →
}
char c;
while(fscanf(fin,"%c",&c) == 1)
{
if (isprint(c))
printf("%c",c);
else
printf("%c",'.');
}
-
Let
str be a string. Write a chunk of code that
replaces all space characters in str with $ characters.
-
Write a function
multiply with the following
prototype
char* multiply(char *s, int n);
that returns a string that is n copies
of s back-to-back. The multiply
function will have to allocate the array as well as populate
it with chars.
-
Use man to lookup
fgetc and ungetc.
Now, explain what the following program does, and why it needs
fgetc and ungetc.
#include <stdio.h>
#include <ctype.h>
int main()
{
char c;
int t = 0, n;
while((c = fgetc(stdin)) && c != EOF)
{
if (isdigit(c))
{
ungetc(c,stdin);
fscanf(stdin,"%i",&n);
t += n;
}
}
fprintf(stdout,"%i\n",t);
return 0;
}
In case it helps, I'll give you an example of this program in action:
bash$ echo "abc123efg456hij" | ./hw2
579