April 2000
CA208 : 'C' PROGRAMMING

QUESTION 2

Total Marks: 15 Marks

Click here to access other questions

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

Do not award half marks. Do not deduct marks for trivial syntactic errors. Alternative correct answers should be given credit, unless otherwise indicated in the marking scheme.


(a)Write a function,called Copy the signature of which is given below,which will copy at most n characters from the string src into the string dst You may assume that dst is a character string with at least n+1 elements.
void Copy(const char* src, char* dst, int n); [5 marks ]

A sample definition of Copy follows:
void Copy(const char* src, char* dst, int n)
{
   while( *src != ’\0’ &&n >0){
       *(dst)++ = *(src)++;
       n--;
   }
   *dst= ’\0’;
}
And the following marking scheme shouldbe used:
•Declaring a suitable iterative structure;(1 mark)
•Bound by the length of the source string and n(1 mark)
•Copying the appropriate character on each iteration,and incrementing the string pointers appropriately;(1 mark)
•Decrementing n on each iteration;(1 mark)
•Ensuring that the destination string is NULL terminated after all characters have been copied.(1 mark)
[5 marks ]

(b)Write a function,called IsMember the signature of which is given below which will return a pointer to the first instance of the character c in the string s If there is no occurrence of the character in the string,then the function should return NULL
char* IsMember(char* s, char c); [5 marks ]

A sample definition of IsMember follows:
char* IsMember(const char* s, char c)
{
   char* t= s;
   while((*t)!= ’\0’) {
      if ((*t) ==c ){
         return t;
      }
      t++;
   }
   return NULL;
}
And the following marking scheme shouldbe used:
•Declaring a suitable iterative structure;(1 mark)
•Bound by the length of the string;(1 mark)
•Correctly comparing the character in the string with the test character,with the correct de-referencing;(1 mark)
•Returning t when a match is found,NULL if the endof the string has been found without a match;(1 mark)
•Incrementing the local string pointer on each iteration.(1 mark)
[5 marks ]

(c)Write a function,called RemoveMember the signature of which is given below,which will take a string s and return a reference to the string with all occurrences of the character c removed from the string.For example,calling the function on the string abcabcabc with the character a to be removed wouldresult in the string bcbcbc.
void RemoveMember(char* s, char c); [5 marks ]

A sample definition of RemoveMember follows:
void RemoveMember(char* s, char c)
{
   int i, j;
   i= 0; j= i+1;
   for(i= 0; i< sizeof(s); i++)
     if(s[i]== c)
       for(j= i; j< sizeof(s); j++)
         s[j]= s[j+1];
}
And the following marking scheme shouldbe used:
•Declaring a loo to iterate over the source string;(1 mark)
•Correctly bound by the length of the string (1 mark)
•Comparing the current character on each iteration with c(1 mark)
Declaring a loop to iterate over the remainder of the string if a match is found; (1 mark)
•Copying the j+1th character into the jth position whilst looping over the
string.(1 mark)
[5 marks ]