Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding length of a string in Fortran

3,082 views
Skip to first unread message

GRIM...@eisner.decus.org

unread,
Sep 26, 1991, 1:36:00 PM9/26/91
to
> How do you determine the length of a character string?
> for example:
> char*50 out_dir
> out_dir = 'my$root:[xyz]'
> I need x = length_of_string(out_dir) ! should be 13
> Is there any simple way.

The easiest way I've found to do this is using the VAX Run-time Library
(RTL) routine STR$TRIM, which is really intended to trim trailing blanks
and tabs from a character variable. Format is:

STATUS = STR$TRIM(destination_string,source_string[,resultant_length])

as specified on page STR-87 of Vol. 5-C of the Programming Doc. Set.
The source and destination strings can be the same.

Seth Grimes, grim...@eisner.decus.org

jim barbour

unread,
Oct 2, 1991, 2:28:13 AM10/2/91
to

As already mentioned, str$trim has several problems, including speed and
system resource consumption. Another problem is that if you write a
stringlength function which uses str$trim, and you call stringlength with a
constant string, the program will crash with an access violation. This
actually happened to me once upon a time.

Usually, if I need to know the length of a string, its one that's been read in
from somewhere, usually the keyboard. In a case like this, the following works
well.

character*80 s
integer*4 len

read '(q,a)',len,s

print *,'You typed in '//s(:len)//' and its length is ',len
end

Hope this is helpful.

Jim Barbour (jwba...@clipr.colorado.edu)

richa...@mwk.uucp

unread,
Oct 5, 1991, 3:47:24 PM10/5/91
to
> How do you determine the length of a character string?
> for example:
> char*50 out_dir
> out_dir = 'my$root:[xyz]'
> I need x = length_of_string(out_dir) ! should be 13
> Is there any simple way.

Here's a routine I wrote to do what you want. Mine treats trailing tabs the
same as trailing spaces.

B. Richards

------------------------------------CUT HERE-----------------------------------
integer*4 function length ( string )
!------------------------------------------------------------------------------
! Written by B. Richards - June, 1991.
!
! This function returns the length of a string once all trailing spaces
! and tabs have been removed.
!
! Arguments:
!
! - string character readonly
! The string whose length is to be returned.
!
! Function returns the length, like a FORTRAN intrinsic function.
!------------------------------------------------------------------------------
implicit integer*4 (a-z)

character*(*) string

character*1 tab / 9 /

lstr = len(string)

do length = lstr, 1, -1
if ( string(length:length) .eq. ' ' .or.
* string(length:length) .eq. tab ) then
continue
else
goto 8000
endif
enddo
length = 0
8000 continue
return
end
--
Bobby Richards - Technical Support - M. W. Kellogg, Houston, Texas
richa...@mwk.uucp
P. O. Box 4557 - Houston, Texas 77210-4557 -- Voice Phone: (713) 753-3429
This line intentionally left blank

Lars Olsson

unread,
Oct 10, 1991, 10:15:36 AM10/10/91
to
Someone wrote:
> How do you determine the length of a character string?
> for example:
> char*50 out_dir
> out_dir = 'my$root:[xyz]'
> I need x = length_of_string(out_dir) ! should be 13
> Is there any simple way.

Why not use the built-in function in the STR$-run-time-library?

--> STR$TRIM(instr,outstr,length)

call str$trim(out_dir,out_dir,x)

In fortran, the out_dir-string is not modified but the length of the
text in the string will be put into x.

/Lasse


--
____________________________________________________________________________
/ Lars Olsson | Internet: o...@ffa.se \
| The Aeronautical Research Institute of Sweden | Voice: +46-8-7591153 |
| Box 11021 | Fax: +46-8-253481 |

Pat Rankin

unread,
Oct 13, 1991, 4:40:23 AM10/13/91
to
> (VAX Fortran
> does not check that the end index of a substring is greater than the start,
> so something that generates a substring of string(1:0) actually access a
> very long string.)

This is doubly wrong. FORTRAN does check this if you compile with
/CHECK=BOUNDS (which is implied if you use /CHECK). 1:0 would give a
"subscript out of range" arithmetic trap. If you don't enable bounds
checking, a substring of length 0 (which is what 1:0 gives) does work
correctly, since it's just the length field in a string descriptor, where
empty strings are perfectly legal. An attempt to get a substring from
2:0 would give one with a length of 65535 (the length calculation results
in a negative number, which when stuffed into the unsigned descriptor
field becomes a large [positive] value). Substrings from 1:0--or N:N-1
in general--do work, but they're documented as illegal so use with care.

Pat Rankin, ran...@eql.caltech.edu

THOM...@usc.pppl.gov

unread,
Oct 12, 1991, 4:16:28 PM10/12/91
to
> How do you determine the length of a character string?
> for example:
> char*50 out_dir
> out_dir = 'my$root:[xyz]'
> I need x = length_of_string(out_dir) ! should be 13
> Is there any simple way.

The following 2 routines find the length a string; the difference between
them is how they handle a string that is totally blank. The first returns
a length of 1 and so is appropriate for situations such as:
type '(2x,a)', string(1:jc_trim(string))
The second routine returns a length of 0 for a blank string. (VAX Fortran


does not check that the end index of a substring is greater than the start,
so something that generates a substring of string(1:0) actually access a
very long string.)

Both routines return a length that ignores any ASCII code <= space, which
includes nulls and tabs.

function jc_trim(string) !Find string length >= 1
character string*(*)
jl=1 !Returns length >= 1
i=len(string)
do while (i.gt.jl)
if(ichar(string(i:i)).gt.ichar(' ')) jl=i
i=i-1
enddo
jc_trim=jl
end

function jc_len(string) !Find string length
jl=0 !blank string returns length 0
i=len(string)
do while (i.gt.jl)
if(ichar(string(i:i)).gt.ichar(' ')) jl=i
i=i-1
enddo
jc_len=jl
end

These routine names are prefixed with "jc" because they were written by
John Coonrod, which conveniently makes them integer functions.
------
Marilee Thompson - Princeton Plasma Physics Laboratory
thom...@usc.pppl.gov

Zack C. Sessions

unread,
Oct 10, 1991, 11:13:30 AM10/10/91
to
richa...@mwk.uucp writes:

>> How do you determine the length of a character string?

[in FORTRAN]

> Here's a routine I wrote to do what you want. Mine treats trailing tabs the
>same as trailing spaces.

[code segment deleted]

Why re-invent the wheel? Doesn't STR$TRIM do what was originally required?


Zack C. Sessions
sess...@seq.uncwil.edu

0 new messages