Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

coutning characters in a string

Status
Not open for further replies.

csiegs

Technical User
Mar 18, 2003
1
0
0
US
I'm using Fortran 77 - I'm trying to count the number of characters in a string that the user enters. The user enters a a path for a file to be stored to, and then I need to append some numbers to the end of it.

Example:
user enters: c:/march18/file
then the following would be created:
c:/march18/file1000.dat
c:/march18/file1001.dat
c:/march18/file1002.dat
and so on..

The problem is that the number of characters in the pathname changes all the time. I can't use concatentate because then I get trailing spaces after 'file' depending on the length that I declare. I dont want to go in and change the code every time I enter a new path. Annoying!

Please help!
Thanks.
 
I'm not sure if it exists within Fortran 77, but in Vax Fortran, there is a function call INDEX where you can find a certain character. If there are no imbedded blanks within the string, you could use INDEX to find the position of the first blank, then subtract 1 to have the end of the string. If INDEX won't work, you could search the string manually from the end and stop when you hit a non-blank character. Something like this. CUR_POS would end up being the last character of the string. In this example, CUR_POS would be zero if the string was blank. Not sure if this will work with Fortran 77, but I think you will get the idea.

CHARACTER*10 STR_VAR
INTEGER CUR_POS
LOGICAL DONE

CUR_POS = 0
IF (STR_VAR.NE.' ') THEN ! Make sure string is not blank
CUR_POS = 10
DONE = .FALSE.
DO WHILE(.NOT.DONE)
IF (STR_VAR(CUR_POS:CUR_POS).NE.' ') THEN
DONE = .TRUE.
ELSE
CUR_POS = CUR_POS - 1
END IF
END DO
END IF
 
INDEX is a standard F77 function. It returns the position of a substring within a string.
LEN is also a standard F77 function. It is supposed to return the length of the string but in some implementations, it returns the size of the identifier. For instance,

Code:
INTEGER strlen
CHARACTER*8 str
str = 'notice'
strlen = LEN(str)

On some implementations, strlen will be 8, on others it will be 6. For concatenation, you can always use slicing with indices eg

Code:
      character*8 one, two
      character*16 three
      integer onelen, twolen
      one = 'leg'
      two = 'end'
      onelen = index(one, ' ')
      twolen = index(two, ' ')
      three = one(1:onelen - 1) // two(1:twolen - 1)
 
Depending on your fortran implementation and OS, you could sometime find special functions like LNBLNK or LEN_TRIM that returns the length of a string passed as argument wihtout couting the trailling blanks. These functions exists in VisualFortran and UNIX (for LNBLNK).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top