December
1999 QUESTION 3 Total Marks: 15 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS |
(a) |
Give a declaration of a string,called my string ,whose length is dynamic. char*my_ string;
|
[1] | |||||||||||||||
(b) |
Write an iterative function,called length ,the signature of which is given below,which takes in a null terminated string x and returns the length of the string. int length(char*x); A sample definition of length follows:
And the following marking scheme should be used:
Incrementing the string pointer and the counter on each iteration;(1 mark) Returning the counter value when the loop terminating condition is reached. (1 mark)
|
[3] | |||||||||||||||
(c) |
Write an iterative function,called char at ,the signature of which is given below, which takes a null terminated string x and a character y and will return the position of the first occurrence of the character y in the string x .The first character in the string is in position 0,and the function should return -1 if the character is not present. int char at(char*x,char*y); A sample definition of char at follows:
And the following marking scheme should be used:
Incrementing the string pointer and the counter on each iteration;(1 mark) Returning
the counter value when a match is found;(1 mark)
|
[4] | |||||||||||||||
(d) |
Briefly explain how your char at function could be adapted such that it would return a reference to the character. You are not required to implement a function which returns this reference. The return type would be changed to a char*,which would return a pointer to the character,should it be present.
|
[1] | |||||||||||||||
(e) |
Write a procedure,called concatenate
,the signature of which is given below,which takes as parameters three
strings.On exit from the procedure,the string z should contain th
concatenation of the two strings x and y .You should use the C library
function malloc ,the signature of which is given below,to allocate
space for a new string.
A sample definition of concatenate follows:
And the following marking scheme should be used:
With the
correct amount of memory allocated by using length ; Declaring a loop which iterates along the first string,correctly bound by a non-null condition;(1 mark) Copying the pointer on each iteration;(1 mark) Declaring a loop which iterates along the second string,correctly bound by a non-null condition;(1 mark) Copying the pointer on each iteration;(1 mark) |
[6] |