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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with string concatenation.

Status
Not open for further replies.

mjmnyr96

Programmer
Apr 25, 2006
7
US
I am fairly new to Fortran and am running into a problem with "strings".

Say I have a couple of variables:

character tempword*20, a*1

Say I have the user go throuh a loop a certain number of times to fill tempword one character at a time. I store the input in a, but how do I now concatenate the contents of a into tempword?

Thanks for the help

mm
 
You can do it either by filling in tempword one character at a time or by concatenation. Say the number of characters in tempword is templen, with initial value 0.
Code:
templen = templen + 1
tempword(templen:templen) = a
The thing you have to remember about concatenation is that it takes the size of the string as the length. Say you have character*20 tempword
Code:
tempword = 'fire and '
tempword = tempword // 'water'
print *, tempword
You will just get fire and . If you wish to concatenate it, you have to use the length but the len function will not tell you how long the string is: it will only tell you how many characters the variable can contain.
To concatenate, the strings on either side must be the exact strings to be catenated: not the variables that contain them
Code:
tempword = tempword(1:9) // 'water'
In your question, this would be very inefficient. It would be equivalent to
Code:
templen = templen + 1
tempword = tempword(1:templen) // a
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top