December 1998
CA208: 'C' PROGRAMMING

QUESTION 3

Total Marks: 20 Marks

Click here to access other questions

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

 

(a) What integer will be returned by the function call strfun1("hello")? Describe the effect of the function when given an arbitrary string as an argument.

int strfun1(char *src) {
   int i;
   for(i=0;*src;(i++,src++)) {}
   return i;
}

[3]
  • five
  • The function strfun1 returns the length of the string src.

 

(b) Give the function call strfun2(dst,"hello","world"), what string will be copied into dst by the function? Describe the effect of the function on the argument dst, if x and y are null terminated strings, and dst is a sufficiently large character array.

void strfun2(char *dst, char *x, char *y) {
   char *ptr;
   for(ptr=x; *ptr; *dst++=*ptr++) {}
   for(ptr=y; *ptr; *dst++=*ptr++) {}
}

[3]
  • "helloworld"
  • The function strfun2 concatenates (i.e., joins) the strings x and y together (1 mark), and writes the concatenated string into dst (1 mark).

 

(c) What integer will be returned by the function call strfun3("hello,"hello")? Describe the effect of the function when given an arbitrary string as an argument.

int strfun3(char *x, char *y) {
   int res=0;
   for(;; (x++,y++)) {
      res+=*x - *y;
      if (!*x || !*y) break;
   }
   return res;
}

[3]
  • zero
  • The function strfun3 returns 0 if the strings x and y are equal, and a non-zero integer otherwise.

 

(d) What integer will be returned by the function call strfun4("hello,"hello")? Describe the effect of the function when given an two arbitrary string as arguments.

int strfun4(char *x, char *y) {
   for(;; (x++,y++)) {
     if (!*x) break;
     if (!*y) break;
     if (*x!=*y) return 0;
   }
   return (*x==*y);
}

[3]
  • zero
  • The function strfun4 returns 1 if the strings x and y are equal (1 mark) otherwise it returns 0 (1 mark).

 

(e) What string will be returned by the function call strfun5("hello", 'l')? Describe the effect of the function when given an arbitrary string and character as an arguments.

char *strfun5(char *src, char c) {
   char *ptr;
   for(ptr=src;*ptr;*ptr++)
      if (*ptr==c) break;

   if (!*ptr) return NULL;
   else return ptr;
}
  

[3]
  • "llo"
  • The function strfun5 returns a pointer to the first occurrence of the character represented by the parameter c, and a NULL pointer if it does not occur in the string.

 

(f) Define an implementation of a function strncpy, with function prototype shown below, which copies at most n characters of the string src into dst. You can assume that dst is a character string with at least n+1 elements.

void strncpy(char *dst, char *src, int n);

[5]
Definition of strncpy:

void strncpy(char *dst, char *src, int n) {
   while (*src!='\0' && n>0) {
     *(dst++) = *(src++);
     n--;
   }
   *dst='\0';
}

and the following marking scheme should be used:

  • defining an iteration statement (may be a while or for-loop), such that the loop is terminated when either a null character is seen (1 mark), or n characters have been copied (1 mark).
  • During the ith iteration of the loop, the  ith character from src needs to be copied into dst (1 mark).
  • During each iteration of the loop, n needs to be decremented (1 mark).
  • After the characters from src have been copied into dst, dst has to be null terminated (1 mark)
  • no marks should be deducted for trivial syntactic errors.