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

Length of Character Strings

195 views
Skip to first unread message

Daniel L Mills

unread,
Apr 1, 1996, 3:00:00 AM4/1/96
to

Hello All,

I am not a Fortran programmer so most will probably find my question very basic.
I need a function that will determine how many non-blank characters there are in a
string (I guess you call, for example a char*256 string, a string). I need to
concatenate to of the guys together so I must determine the number of non-blanks
in the first so they can be ingnored. I'm using the f77 compiler on a Sparc 10
running OS 4

Much thanks for any help.

--Dan
--
Daniel L Mills (Dan)
University of Maryland
College Park, MD 20742

Kenneth Plotkin

unread,
Apr 1, 1996, 3:00:00 AM4/1/96
to
In article <4joo6m$8...@ancona.umd.edu>,
Daniel L Mills <dmi...@Glue.umd.edu> wrote:

[snip]


>concatenate to of the guys together so I must determine the number of non-blanks
>in the first so they can be ingnored. I'm using the f77 compiler on a Sparc 10

[snip]

I assume you're looking to delete trailing blanks, not blanks anywhere. I
also assume the trailing blanks will be spaces, and not nulls.

F77 has an intrinsic function LEN, which will do you no good because all
it reports is the declared length of the string.

Browse your Fortran manual carefully to see if it has a blank trimming
function. Some do. Microsoft Fortran on a PC has function len_trim,
which I believe is actually a borrowed C function.

The traditional method is to search from the end of the string back:

do 10, i = length,1,-1
if(string(i:i).ne.' ') go to 20
10 continue
20 continue
noblnk = i

with some suitable logic between 10 and 20 in case the whole thing is
blank.

If your strings will really have no blanks other than trailing ones, you
can do the following:

noblnk = index(string//' ',' ')

where INDEX is a standard F77 intrinsic that, for index(a,b), gives the
position of string b in string a. (I may have a,b backwards; the manual
is not right at hand.)

Ken Plotkin

CHI Research, Inc.

unread,
Apr 1, 1996, 3:00:00 AM4/1/96
to
In <4joo6m$8...@ancona.umd.edu> dmi...@Glue.umd.edu (Daniel L Mills)
writes:
>
>
>Hello All,
>
>I am not a Fortran programmer so most will probably find my question
very basic.
>I need a function that will determine how many non-blank characters
there are in a
>string (I guess you call, for example a char*256 string, a string). I
need to
>concatenate to of the guys together so I must determine the number of
non-blanks
>in the first so they can be ingnored. I'm using the f77 compiler on a
Sparc 10
>running OS 4
>
>Much thanks for any help.
>
>--Dan
>--
>Daniel L Mills (Dan)
>University of Maryland
>College Park, MD 20742

I think you don't really want the number of non-blanks, but the last
non-blank. AS far as I know, there is nothing like this packed into
standard Fortran. But it is easy to write a routine for it.

Dom Olivastro


Sergio Gelato

unread,
Apr 2, 1996, 3:00:00 AM4/2/96
to
In article <4jp9bb$7...@access2.digex.net>,
Kenneth Plotkin <kplo...@access2.digex.net> wrote:
>[snip]

>>concatenate to of the guys together so I must determine the number of non-blanks
>>in the first so they can be ingnored. I'm using the f77 compiler on a Sparc 10
>[snip]

>Browse your Fortran manual carefully to see if it has a blank trimming
>function. Some do. Microsoft Fortran on a PC has function len_trim,
>which I believe is actually a borrowed C function.

Ken, Ken, you _really_ should take a look at F90. len_trim(string) is a
new STANDARD intrinsic function. (There is also trim(string), which is a
shorthand for string(:len_trim(string)).)

Sun's f77 has neither trim nor len_trim (at least not in the versions I have
used), which is why I use the following piece of code (not quite 77 standard-
conforming, but all the compilers I use these days accept it):

* $Id: lentrm.f,v 1.1 1996/01/26 19:41:02 gelato Exp $

* While we wait for LEN_TRIM(), here are a couple of
* substitutes. LENTRM() is a strict equivalent of LEN_TRIM(),
* while LENTRM1() returns 1 when LENTRM() would return 0.

INTEGER FUNCTION lentrm (s)
IMPLICIT NONE
CHARACTER*(*) s

INTEGER i
DO i=len(s), 1, -1
IF (s(i:i).NE.' ') THEN
lentrm = i
RETURN
END IF
END DO
lentrm = 0
END

INTEGER FUNCTION lentrm1 (s)
IMPLICIT NONE
CHARACTER*(*) s

INTEGER i
DO i=len(s), 2, -1
IF (s(i:i).NE.' ') THEN
lentrm1 = i
RETURN
END IF
END DO
lentrm1 = 1
END

There. The only reason for having lentrm1 is that zero-length strings
were not standard in Fortran 77. max(1,lentrm(s)) is more of a mouthful.

>If your strings will really have no blanks other than trailing ones, you
>can do the following:

> noblnk = index(string//' ',' ')

You got the order of arguments right, but the concatenation will not work
if string is character*(*). (We are talking Fortran 77, aren't we?)
Besides, I think your answer will be off by one.
Instead, one could code
noblnk = index(string,' ')-1
if (noblnk.lt.0) noblnk = len(string)
although I personally never do.
--
Sergio Gelato

Lucio Chiappetti

unread,
Apr 2, 1996, 3:00:00 AM4/2/96
to

|> In <4joo6m$8...@ancona.umd.edu> dmi...@Glue.umd.edu (Daniel L Mills)
|> writes:
|> >I need a function that will determine how many non-blank characters
|> there are in a
|> >string

Here you go. OK for f77.
f90 has a builtin, I believe ...


FUNCTION TRUE_LENGTH(STRING)
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: Function TRUE_LENGTH
C.LIBRARY: GENERAL
C.AUTHOR: L.Chiappetti - IFCTR Milano
C.VERSIONS: 1.0 - 28 May 91 - Original version
C.PURPOSE: return the true length of a string
C.METHOD: count last non-blank character in string
C.SYNTAX: length = TRUE_LENGTH(string)
C.PARAMETERS: CHARACTER*(*) string a string (in)
C- INTEGER return value the true length (return)
C.RESTRICTIONS: none
C.NOTES: none
C.FILES: none
C.REFERENCES: noen
C
C----------------------------------------------------------------------
C
C INCLUDE 'implicit_none.inc'
CHARACTER*(*) STRING
INTEGER TRUE_LENGTH
INTEGER I
C
DO 1 I=LEN(STRING),1,-1
IF (STRING(I:I) .NE. ' ') THEN
TRUE_LENGTH=I
RETURN
ENDIF
1 CONTINUE
TRUE_LENGTH=0
RETURN
END

--
----------------------------------------------------------------------------
A member of G.ASS : Group for Astronomical Software Support
----------------------------------------------------------------------------
Lucio Chiappetti - IFCTR/CNR | Ma te' vugl' da' quost avis a ti' Orsign
via Bassini 15 - I-20133 Milano | Buttet rabios intant te se' pisnign
Internet: LU...@IFCTR.MI.CNR.IT | (Rabisch, II 46, 119-120)
----------------------------------------------------------------------------
For more info : http://www.ifctr.mi.cnr.it/~lucio/personal.html
----------------------------------------------------------------------------

rfri...@vcnet.com

unread,
Apr 2, 1996, 3:00:00 AM4/2/96
to
> dmi...@Glue.umd.edu (Daniel L Mills) writes:
>
> Hello All,
>
> I am not a Fortran programmer so most will probably find my question very basic.
> I need a function that will determine how many non-blank characters there are in a
> string (I guess you call, for example a char*256 string, a string). I need to
> concatenate to of the guys together so I must determine the number of non-blanks
> in the first so they can be ingnored. I'm using the f77 compiler on a Sparc 10
> running OS 4
>
> Much thanks for any help.
>
> --Dan
> --
> Daniel L Mills (Dan)
> University of Maryland
> College Park, MD 20742
>
>>>>
I'm going to assume you mean what is the last non-blank character in the string.
A simple Fortran funciton I've often used is:

INTEGER FUNCTION LENG(STRING)
CHARACTER *(*) STRING
INTEGER WORK
INTEGER I
WORK = LEN(STRING)
DO 10 I = WORK,1,-1
IF(STRING(I:I) .NE. ' ') GOTO 20
10 CONTINUE
20 CONTINUE
IF(WORK .GT. 0) THEN
LENG = WORK
ELSE
LENG = 1
END IF
RETURN
END
Clearly if whole string is blank, the correct answer is 0. Howver, I often use constructs
such as

CHARACTER*256 BIG
CHARACTER*20 LITTLE
CHARACTER*266 ANSWER


ANSWER = BIG(1:LENG(BIG))//LITTLE(1:LENG(LITTLE))

and I'd get an error if I referenced a substring STRING(1:0)

remember, in Fortran STRING(I;J) means the substring consisting of
characters I through J inclusive, and you must have 1<= I<=J<=LEN(STRING)

LEN is a standard F77 routine which returns the length of the string as set
in the CHARACTER statement.
good luck


_______________________
|Ralph Jay Frisbie |
|A flying saucer lover, |
|but not the inventor. |
|rfri...@vcnet.com |
|-----------------------

Ron Sverdlove x2517

unread,
Apr 3, 1996, 3:00:00 AM4/3/96
to
In article <4jp9bb$7...@access2.digex.net>, kplo...@access2.digex.net (Kenneth Plotkin) writes:
>Browse your Fortran manual carefully to see if it has a blank trimming
>function. Some do. Microsoft Fortran on a PC has function len_trim,
>which I believe is actually a borrowed C function.
>

len_trim is not a C function. It is the standard Fortran 90 function for this purpose.
But in Fortran 90 there is also the trim function. To concatenate two strings omitting
the trailing blanks from the first one, you can say:

c = trim(a) // b

and not have to worry about the number of non_blanks at all.

To respond to the original question, Sun Fortran has a function called lnblnk
which gives the number of characters in the string up to the last nonblank one.


--
Ronald Sverdlove Computational Science Research
r...@sarnoff.com David Sarnoff Research Center
Tel. 609-734-2517 CN 5300
FAX 609-734-2662 Princeton, NJ 08543-5300

Loren Meissner

unread,
Apr 4, 1996, 3:00:00 AM4/4/96
to
Switch to Fortran 90 and use the intrinsic function "Len_Trim".
The function reference LenTrim( String ) returns the length of
String with trailing blanks removed.

Long_String = String_A(: Len_Trim(String_A)) // String_B

If you are going to do an assignment as shown here, there is no
reason to compute the length of String_B.

--Loren Meissner

Ron Sverdlove x2517

unread,
Apr 5, 1996, 3:00:00 AM4/5/96
to
In article <00001a89...@msn.com>, LPMei...@msn.com (Loren Meissner) writes:

>Long_String = String_A(: Len_Trim(String_A)) // String_B
>
>If you are going to do an assignment as shown here, there is no
>reason to compute the length of String_B.

Better yet, use

Long_String = Trim(String_A) // String_B

There's no need to explicitly compute the non-blank length of String_A, either.

Michel OLAGNON

unread,
Apr 5, 1996, 3:00:00 AM4/5/96
to
In article <00001a89...@msn.com>, LPMei...@msn.com (Loren Meissner) writes:
>Switch to Fortran 90 and use the intrinsic function "Len_Trim".
>The function reference LenTrim( String ) returns the length of
>String with trailing blanks removed.
>
>Long_String = String_A(: Len_Trim(String_A)) // String_B
>
>If you are going to do an assignment as shown here, there is no
>reason to compute the length of String_B.

It seems to me that for this example, it is simpler to write:

Long_String = Trim(String_A) // String_B

--
| Michel OLAGNON email : Michel....@ifremer.fr|
| IFREMER: Institut Francais de Recherches pour l'Exploitation de la Mer|

Kenneth Plotkin

unread,
Apr 5, 1996, 3:00:00 AM4/5/96
to
In article <4jr3s7$4...@ictpsp10.ictp.trieste.it>,
Sergio Gelato <gel...@oort.ap.sissa.it> wrote:

>Ken, Ken, you _really_ should take a look at F90. len_trim(string) is a

[snip]

One of these days I'm sure I will - like when I get a new compiler that
happens to be F90. That will probably be after I begin using Windows 95.
(Insert rant about W95.) That switch (even a switch to F90 by itself)
will be a pretty expensive deal, so it's something I'm not going to seek
out.

In the meantime, the F77 compiler I use most of the time has len_trim, and
if it didn't I'd use something like the program you posted, which is
pretty much the first idea I floated. My version does use labels for the
DOs, not ENDDOs. :-)

[snip]
> noblnk = index(string,' ')-1
[snip]

is correct. I left off the -1; my brain was consumed by trying to
remember the order of arguments in INDEX.

Ken "the old ways are the good ways" Plotkin

0 new messages