August 2000
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) Declare a pointer to an array of characters, called MyChar, the length of which should not be decided until allocation time. [1 mark]
(a) char* myChar[]; [1 mark]

(b) Give the results of executing the following function calls, and explain your answer:
(i) printf("%d\n", strlen("Hello World\n")); [2 marks]
(ii) printf("%d\n", sizeof("Hello World\n")); [2 marks]
(b) (i) • 12 (1 mark)
• This is the length of the string, including the newline but excluding the trailing null; (1 mark)
[2 marks]
(ii) • 13 (1 mark)
• This is the length of the string, including the newline and the trailing null; (1 mark)

(c) Implement a function, called Convert, which takes a pointer to a string s, and converts all of the lower case characters in the string to upper case characters. All upper case characters should remain unchanged. Note that the ASCII values of A to Z is the range 65 to 90, and a to z is 97 to 122. [5 marks]
(c) A sample definition of Convert follows:
void Convert(char* s) {
  for(s; s!= NULL; s++)
  if(atoi(s) >= 65 && atoi(s) <= 90)
  *s= itoa(atoi(s)+32);
}
And the following marking scheme should be used:
• An appropriate function signature, complete with const pointer; (1 mark)
• A loop to iterate along the strong, bound by the null terminating character; (1 mark)
• A test to see if the current character is in the required range, by taking the ascii value of the character; (1 mark)
• Calculating the new ascii value; (1 mark)
• Assigning the new character having calculated it from its ascii value; (1 mark)
[5 marks]

(d) Implement a function, called StrCaseCmp, such that it takes in two pointers to strings, s and t and compares the two strings, ignoring the case of the individual characters. The function should return an integer less than, equal to or greater than zero if s is found, respectively, to be less than, equal to or greater than t. [5 marks]
(d) A sample definition of StrCaseCmp follows:
int StrCaseCmp(const char* s, const char* t) {
  char *a= (char*)malloc(strlen(s));
  char *b= (char*)malloc(strlen(t));
  strcpy(a, s);
  strcpy(b, t);
  Convert(a);
  Convert(b);
  return strcmp(a, b);
}
And the following marking scheme should be used:
• A suitable function signature complete with const parameters; (1 mark)
• Declaring local temporary strings to work with, and ensuring they are long enough;
(1 mark)
• Copying the parameters into the local strings; (1 mark)
• Converting both strings to upper case characters only; (1 mark)
• Returning the result of comparing both upper case strings; (1 mark)
[5 marks]