XML

kent logo

CO631 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for string-handling

2004

Question 66 (2004):

I "think" I am nearly there with q6 I just have one problem left. If all the names are the same size then my code works fine :-). However if the names are different sizes it goes a bit wonky. Say I have:

    Mandy 551
    Mandy 976
    Sonya 577
    Hubert 969
    Mandy 127

And I have "[INT]BYTE temp:", where the max length for a name is 8 and a space is represented as ~, "temp" would hold values something like this:

    1) Mandy~~
    2) Mandy~~
    3) Sonya~~
    4) Hubert~
    5) Mandyt~

The `t' of "Hubert" is not being cleard when I push "Mandy" into it. Well thats all I can come up with why it is not working. How would I clear a string to make it all spaces?

Any tips would be great!

Answer 66:

To clear all the elements of a byte array to spaces is trivial (and obvious!):

    SEQ i = 0 FOR SIZE my.array
      my.array[i] := ' '

But it sounds like you're not calling "make.string" correctly. Whenever you read from the tagged protocol channel into the byte array (along with the number of bytes, say "count", communicated for the name and the associated mark), only the first "count" elements of the array are changed -- the rest are not touched. You must call the "make.string (name, count)" procedure, where "name" is the name of the byte array. This clears any bytes left over in "name" to zero (the ASCII NUL character) -- that is all bytes apart from the first "count" elements. That will erase your left-over `t~'.

Keywords: q6 , string-handling


Question 55 (2004):

In an previous anonymous question someone asked how to compare strings. The answer said to pass these BYTE arrays through the "make.string" function and then use "compare.string". However, I get the error:

    make.string is not a function

Is this a provided function or do we have to make it ourselves ?

Answer 55:

"make.string" is provided, but it's not a function -- it's a procedure. It adjusts the BYTE-array given to it as a parameter so that the string can be used with the other string functions -- which are really functions (e.g. "compare.string" and "equal.string"). The the answer to Question 50 (2003) for a code example on how to use it.

Keywords: q6 , string-handling

2003

Question 71 (2003):

For:

    PROC make.string([]BYTE a, VAL INT length)

it doesnt output to anything it seems, unlike the `copy.string'

does this means it just changes the variable passed to it for the string part to the length that is passed to it?

ie if we had:-

    []BYTE name1:
    INT length1:

    make.string(name1, length1)

would this change the variable `name1' to be the length passed to it?

Answer 71:

Yes, it modifies the contents of the array passed to it as a parameter. The array itself is left untouched.

In your code fragment, you have this:

    []BYTE name1:

This is an illegal declaration. The array-size must be given, e.g.:

    [max.name.length]BYTE name1:

Also, when you call `make.string (name1, length1)', it's important that `length1' has been initialised -- perhaps to zero.

Keywords: string-handling


Question 68 (2003):

is an empty string represented as ""

e.g. `equal.string(name,"")' would check if the name varible was empty

Answer 68:

Yes, that comparison will return TRUE, if at some point, previously, this happened:

    make.string (name, 0)

Keywords: q6 , string-handling


Question 50 (2003):

To use `equal.string', I understand that I have to make the string from the array of bytes first using `make.string', but whenever I try to use `make.string' the compiler tells me that `make.string' is not a function. And indeed when I look at `string.occ' in the libsrc directory it is a PROC not a FUNCTION. Do I need to re-write `make.string' as a function or is there another way ??

Answer 50:

`make.string' adjusts an existing string (array of BYTEs) -- it does not create a new one. The function of this (if you read the source code) is to replace all the non-string bytes with a special token (ASCII NUL). In effect, it allows other routines to work out the length of the string -- which will likely be different from the size of the array.

For example:

    PROC string.test ()
      [15]BYTE s1:
      [10]BYTE s2:
      SEQ
        --{{{  initialise s1
	[s1 FOR 5] = "Hello"
	make.string (s1, 5)
	--}}}
	--{{{  initialise s2
	s2 = "HelloWorld"
	make.string (s2, 5)
	--}}}
	IF
	  equal.string (s1, s2)
	    good ()
	  TRUE
	    bad ()
    :

See also the answers to Question 48 (2003) and Question 19 (2001).

Keywords: string-handling

Referrers: Question 55 (2004)


Question 48 (2003):

I'm stuck on q6. My program deadlocks and I don't know why. I have managed to work out that it is something to do with my `cell' procedure, and also it appears to me that the `string.equals' procedure doesn't work in my cells. Any help would be great thanks.

    [snip code]

Answer 48:

Questions such as this are not easily answered in this public forum. Consider emailing your seminar leader.

However, in response to your query, when using `equal.string', etc., you must first ensure that `make.string' has been used on the string. These strings are not really strings -- they are arrays of BYTEs. `make.string' adjusts the array so that comparisons, etc. are based on the length of the `string', not the size of the array.

Keywords: q6 , string-handling

Referrers: Question 50 (2003)

2001

Question 19 (2001):

For Q6, if I have two strings called a and b and I want to use the compare.string function on them, how do I do it? Is it something like:

  compare.string (a, b)

Also, if I get back string.less or string.more, which of the two strings is being referred to as smaller or bigger than the other?

Answer 19:

Yes to your first question. Make sure you have read the documentation in libsrc/string.occ, which is part of the course library and which contains the string handling routines. Most important is that functions like compare.string (etc.) do not work unless the BYTE arrays on which they operate have first been processed by make.string.

To your second question, if compare.string (a, b) returns string.less, then a is less than b. That was meant to be obvious ... if you were at all unsure, a glance at the source code would have confirmed this ...

Note: if others have further questions about this assignment, please first refer to last year's Question 51 (2000) onwards.

Keywords: q6 , string-handling

Referrers: Question 50 (2003) , Question 54 (2003)

Valid CSS!

Valid XHTML 1.0!

Last modified Mon May 15 17:39:48 2006
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.