Tutorial Week 6
Write a program using an array to read in a set of numbers terminated by EOF
and print them out in reverse order. Assume that no more than 100 numbers need
to be read.
Ans:
#include
#define MAXNUM 100
int main(int argc, char *argv[])
{
int numbers[MAXNUM];
int number, count;
count = 0;
while (count < MAXNUM && scanf("%d", &number) != EOF) {
numbers[count++] = number;
}
while (count > 0)
printf("%d\n", numbers[--count];
exit 0);
}
Write a function to emulate strcmp to compare two null-terminated strings.
It should return -1 if the first string is lexicographically less than the
second, 1 if it is greater, and 0 if they are the same. For the second assignment,
note the library function strncmp that compares a maximum number of elements
of the strings.
Ans:
int strcmp(char *s1, char *s2)
{
while (*s1 != '\0' && *s2 != '\0') {
if (*s1 < *s2) return -1;
if (*s1 > *s2) return 1;
s1++;
s2++;
}
if (*s1 != '\0') return 1; /* s1 still has chars */
if (*s2 != '\0') return -1; /* s2 still has chars */
return 0;
}
Write a program to store an array of records containing student names and IDs.
The program should read upto 100 records. It should then enter a loop reading
student IDs and printing the corrresponding name if found.
Ans:
#include
struct student_t {
char *name[100];
int ID; /* will need to be long if int is 16 bits */
} students[MAXSTUDENT];
etc
Write a program to read in strings (one per line), store pointers to them in
an array and print them out. NB:You will need to use ``malloc'' to create heap
space to store copies of the strings.
Ans:
If you don't use malloc, but just use one string, everything in the array will
end up pointing to the string.
#include
#define MAX 100
int main(void)
{
char *strings[MAX];
char str[1024]; /* hope this is long enough */
int count = 0;
int n;
while (count < MAX && scanf("%1023s", str) != EOF) {
strings[count] = malloc(strlen(str) + 1); /* space for null char */
strcpy(strings[count], str);
count++;
}
for (n = 0; n < count; n++)
puts(strings[n]);
exit(0);
}
Laboratory Week 6
Use the online manual to read the entry in section three for ``getwd''. Write
a program to print your current working directory. Note the use of MAXPATHLEN
- no complete pathname is allowed to be longer than that.
Ans:
#include
#include
int main(void)
{
char dir[MAXPATHLEN];
if (getwd(dir) == NULL) {
fprintf(stderr, "cant find current dir\n");
exit(1);
}
puts(dir);
exit(0);
}
Use the online manual to read the entry in section two for ``chdir''. Write
a program to read directory names from standard input until end-of-file and
change the current working directory. What patterns are recognised out of `.',
`..', `~', etc?
Ans:
This will recognise ., .. patterns but not ~ - only the shells do that one
too. MAXPATHLEN is used again to give a reasonable size.
#include
#include
int main(void)
{
char path[MAXPATHLEN];
while (gets(path) != NULL) {
if (chdir(path) == 0)
printf("changed to %s\n", path);
else printf("failed to change to %s\n");
}
exit(0);
}
Write a program to read a single line of input and execute this as a command
using the call ``system''. Can this handle ``cd'', or must you treat this as
a special case?
Ans:
Each command is handled by a new shell - chdir cahnges for the new shell which
then exits, so it has no effect on the current environment. You can combine
the last three solutions to get a simple interpreter of your own.
#include
int main(void)
{ char path[2048]; /* they'll never type this much... */
while (gets(path) != NULL)
system(path);
exit(0);
}