CSM210
Answer any two questions. Each question carries a total of 50 marks.
Note: In each of the functions you write, if a string needs to be modified or created then it may be stored in any string which has been passed to the function.
1
pattern_match(char *pattern, char *string)
where
string
is an arbitrary character string and pattern
is a character string
composed of the characters X, 9, and the special symbols - (hyphen), . (dot), /
(slash) and the space character.
pattern-match
returns a 1 if wherever there is an X in pattern
there is a
alphabetic character in string
, wherever there is a 9 in pattern
there is a
digit in string
, and wherever there is a special symbol in pattern
, the same
symbol occurs in string
. pattern-match
returns a 0 if
string
does not match the
pattern and -1 if pattern
contains characters other than those
specified
above.
Examples:
pattern-match("99-X.99", "11/g.87") = 0.
pattern-match("99*L", "99*L") = -1.pattern-match("X999 XXX", "G567 FOL") = 1.
2
tail(char *string1)
, which returns a
pointer to the
string string1
without the leading character. E.g., tail("hello")
returns a
pointer to the string "ello".
(b). Write the function strncmp(char *string1, char *string2, int n)
, which
returns 1 if the first n
characters in string1
exactly
match the first n
characters in string2
, and 0 otherwise.
(c). Write the function substr(char *string, int m, int n)
which
returns a
pointer to a string composed of the substring from the m
th to the n
th
characters of string
. Include error checking. What should substr()
return if an
error has been detected?
3
membern(char character, char *string2, int n)
which
returns 1 if character
occurs exactly n
times in string2
, and 0 otherwise.
(b). Define a data structure that can be used to store a linked list of characters.
(c). Write a C statement which requests space to store a new instance of the data structure defined in 3(b).
(d). Assuming that the last item in the list points to NULL, write a function which traverses the linked list in sequence, printing out each character stored in the list.