2012年11月9日 星期五

Scilab fundamentals 13 - Strings part 1

    Scilab is capable of processing strings such as combination, conversion, and separation. The following two sections will introduce several functions listed in Table 1 that are useful in programming.
 

 
²  ascii: string/ASCII code conversion
 
    The function ascii converts a string or a matrix of string into ASCII codes, or convert ASCII codes into a string.
 
 
     As we can see from Example 1, the string variable A (desperado) consists of 9 characters, and we got 9 corresponding numbers saved in B. Consequently, we got the string variable C which is the same with string variable A, because one ASCII code corresponds to one character. However, what will it be when we convert a matrix of string into ASCII codes by ascii?
 
 
The dimension of matrix A is 2×1, but the dimension of B is 1×19. Obviously, ascii converts a string matrix into ASCII codes, and save all codes in a row vector.
 
²  strtod: convert string to double
    The function strtod converts string to double. The example below shows that a number 300 defined as a string variable is converted to a real variable, num.
 
 
     In the above, we check the data type of real variable num by summing num and 2. The answer is 302. However, does strtod work well when a string consists of a number and characters (I call it compound string)?
 
 
    In the above example, case 1 is a string comprises a number followed by several characters and case 2 is the opposite pattern. The results indicate that strtod can convert a compound string well when the number we want to convert at the beginning (case 1). Otherwise, strtod returns a value of zero (case 2).
 
²  strcmp: compares strings
    The function strcmp compares first string against second string and returns an integer to indicate the relationship between 2 strings. The returned integer is 0 when first string is equal to second string. Otherwise, the absolute value of returned is 1.
 
The returned values of case 1 and case 2 are 1 and -1, respectively, because str1 is not equal to str2 in both cases. A positive value indicates that the first character that has a greater value in str1 than that in str2, and a negative value indicates the opposite. Case 3 shows the comparison of str1 and str3. The result indicates that the comparison mode is case dependent. We can switch the comparison mode to be case independent, as illustrated in case 4.


沒有留言:

張貼留言