19.5.4. Character Handling

The following section outlines some useful character handling functions and subroutines that operate on Fortran character variables.

Functions are provided for carrying out internal formatting in a standard manner, without using Fortran format statements. Thus, you can convert numbers (double precision, integer or real) to and from their character representations. Using the string editing facilities it is possible to remove redundant spaces from a character string, find the active length of a character string, and split a character string into separate strings by treating spaces as separators.

19.5.4.1. CFROMD

      CHARACTER*(*) FUNCTION CFROMD (DVAL)
      DOUBLE PRECISION DVAL (input)

This function converts a double precision number DVAL into a character string, of the form ‘ -1.2345678D+09’ (in this case NFIG=8). The number of figures contained in CFROMD is determined by NFIG. In the calling subroutine, CFROMD must be declared EXTERNAL and to be CHARACTER*n where n is at least of size NFIG+7.

Note that CFROMD produces a right-justified character string (that is, padded with leading blank spaces).

19.5.4.2. CFROMI

      CHARACTER*(*) FUNCTION CFROMI (IVAL)
      INTEGER IVAL (input)

This function converts an integer IVAL into a character string, of the form ‘ -123456’. The number of figures contained in CFROMI is chosen purely according to the value of IVAL. In the calling subroutine, CFROMI must be declared EXTERNAL and to be CHARACTER*n where n is at least of size m+2, where m is the maximum number of figures that will be needed to represent any integer IVAL.

Note that CFROMI produces a right-justified character string (that is, padded with leading blank spaces).

19.5.4.3. CFROMR

      CHARACTER*(*) FUNCTION CFROMR (RVAL)
      REAL RVAL (input)

This function converts a real number RVAL into a character string, of the form ‘ -1.2E+05’ (in this case NFIG=2). The number of figures contained in CFROMR is determined by NFIG as described above. In the calling subroutine, CFROMR must be declared EXTERNAL and to be CHARACTER*n where n is at least of size NFIG+7.

Note that CFROMR produces a right-justified character string (that is, padded with leading blank spaces).

19.5.4.4. DFROMC

      DOUBLE PRECISION FUNCTION DFROMC (CVAL)
      CHARACTER*(*) CVAL (input)

This function converts a character string CVAL into a double precision number. If CVAL is not a valid string for producing a double precision number then the result is 0.

19.5.4.5. IFROMC

      INTEGER FUNCTION IFROMC (CVAL)
      CHARACTER*(*) CVAL (input)

This function converts a character string CVAL into an integer. If CVAL is not a valid string for producing an integer then the result is 0.

19.5.4.6. RFROMC

      REAL FUNCTION RFROMC (CVAL)
      CHARACTER*(*) CVAL (input)

This function converts a character string CVAL into a real number. If CVAL is not a valid string for producing a real number then the result is 0.

19.5.4.7. LENACT

      INTEGER FUNCTION LENACT (CSTR)
      CHARACTER*(*) CSTR (input)

This function gives the position of the last character in the string CSTR, which is not a space. Thus, it gives the active length of the string.

LENACT must be declared EXTERNAL in any calling subroutine.

19.5.4.8. EDIT

      SUBROUTINE EDIT (CSTR, IL, IU, COM, IAC)
      CHARACTER*(*) CSTR (input/output)
      INTEGER IL,IU (input)
      CHARACTER*(*) COM (input)
      INTEGER IAC (output)

This subroutine edits a substring of CSTR. The action taken depends on the value of the command string, COM. Four possible actions can be taken:

  • COM=’U’convert to upper case;

  • COM=’L’convert to lower case;

  • COM=’S’remove all spaces;

  • COM=’C’remove leading and trailing spaces and compress all other contiguous spaces to a single space.

The edit is performed on a substring of CSTR determined by the values of IL and IU. The edit starts at character, I1, and ends at character, I2, where these are determined as follows. I1 is the maximum of IL and 1; therefore, if IL is less than or equal to zero the first character in CSTR is the beginning character. I2 is the minimum of the string length of CSTR and IU. Additionally, if IU is less than or equal to zero, I2 is set to the length of CSTR. If I2 is less than I1, then no edit is performed.

IAC is returned with the length of the substring edited if COM is ‘L’ or ‘U.’ If COM is ‘S’ or ‘C’ it is returned with the active length of the substring edited, see LENACT above.

Note that if COM is not set to a valid character, then EDIT simply returns without taking any action.

Note that MAPCHA must be called before EDIT can be used.

19.5.4.9. IOPNAM

      SUBROUTINE IOPNAM (CNAME, LENSTR, NCHAR, NNAME, LFULL, MXNAME)
      CHARACTER*(*) CNAME (input)
      INTEGER LENSTR, MXNAME (input)
      INTEGER NCHAR(MXNAME), NNAME (output)
      LOGICAL LFULL (output)

This subroutine locates words between slashes inside a string, and is useful for parsing data area pathnames. It takes the pathname given in CNAME and returns a pointer to the start of each part of the pathname within the string CNAME, in the array NCHAR, starting at the left-hand end of the string.

MXNAME is used as the dimension of NCHAR and should be set to the maximum number of names anticipated in the pathname.

LENSTR is the active length of the string CNAME and should be determined by calling LENACT for CNAME prior to the call to this routine.

NNAME provides a count of the separate names in the pathname.

The logical flag LFULL is set to indicate whether the pathname provided is a full one or a relative one. LFULL=.TRUE. if the first character of CNAME is a ‘/’ and .FALSE. otherwise.

For instance, the pathname (CNAME) ‘/TIME0/VEL/UVEL’ would return:

LFULL=.TRUE. (It is a full pathname.)

NCHAR(1) = 2 (The first name in the path (TIME0) starts at character 2 of CNAME.)

NCHAR(2) = 8 (The second name in the path (VEL) starts at character 8 of CNAME.)

NCHAR(3) = 12 (The third name in the path (UVEL) starts at character 12 of CNAME.)

NNAME=3 (There are 3 names in the pathname.)

19.5.4.10. CCATI

      CHARACTER*(*) FUNCTION CCATI(STRING,NUMBER)
      CHARACTER*(*) STRING:- String prefix.
      INTEGER NUMER:- Number to be post-fixed as a string.

This function is used to concatenate a string and number. CCATI must be declared external in the routine calling it and must be typed to a length sufficient to hold the concatenated string and number. If CCATI is too short to hold the result, then it will be filled with asterisks. If CCATI is longer than necessary then it is padded to the right with blanks. For example, if STRING=’FCS’ and NUMBER = 4 then CCATI returns ‘FCS4.’