December 1999
CA208 : 'C' PROGRAMMING

QUESTION 3

Total Marks: 15 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 3

(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:

int length(char*x)
{
  int count;
for(count= 0; *x;(x++,count++)) { }
return count;
   

And the following marking scheme should be used:


•Declaring a suitable iterative structure which is bounded by the non-null character condition;(1 mark)

•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:

int char_at(char*x;char*y)
{
  int count;
for(count=0;*x;(*x++,count++))
   
if(*x==*y)return count;
 
return -1;
}    

And the following marking scheme should be used:


•Declaring a suitable iterative structure which is bounded by the non-null character condition;(1 mark)

•Incrementing the string pointer and the counter on each iteration;(1 mark)

•Returning the counter value when a match is found;(1 mark)
•Returning -1 if no 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.
You are not required to implement malloc .


void *malloc(int size);
void concatenate(char*x,char*y,char*z);

A sample definition of concatenate follows:

void concatenate(char*x,char*y,char*z)
{
 

char*ptr;


‘z= (char*)malloc(length(x) + length(y));

for(ptr=x; *ptr; *z++=*ptr++) { }
for(ptr=y; *ptr; *z++=*ptr++) { }

}  

And the following marking scheme should be used:


•Using malloc to allocate enough memory in the destination string;(1 mark)

•With the correct amount of memory allocated by using length ;
(1 mark)

•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]