This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Handout Four October 24, 2011 1 Characters & Strings in Fortran 1.1 Declaration In handout two of your notes (section 2.3) you were briefly shown how to declare various character types. A table similar to the following was given as correct examples of character declarations. There are two types of character declarations, a ‘single’ character or a ‘string’ of characters. The ‘string’ of characters is achieved by including the (‘ LEN ’) length specifier to the declaration statement. You should also be aware that when dealing with ‘ CHARACTER ’ types the single quote character ( ’ )or the double quote character ( " ) can be used as the delimiters for the character type. 1. CHARACTER :: char1,char2,letter="I" 2. CHARACTER, PARAMETER :: a="a",b="b",c="c" 3. CHARACTER (LEN=6) :: class1=’MT3501’, class2=’MT3502’,class3=’MT3503’ 4. CHARACTER (LEN=*), PARAMETER :: headline="Man United will win the league?" 5. CHARACTER (LEN=5), PARAMETER :: name="Steve" • The first declaration declares three single ‘ CHARACTER ’ variables the third of which is initialised to be the letter ‘ I ’. • The second declaration statement declares three single ‘ CHARACTER ’ objects and declares them with the ‘ PARAMETER ’ attribute setting them to the first three letters of the alphabet. Remember data types declared with the ‘ PARAMETER ’ attribute can not have there contents changed anywhere in the program, often referred to as constants. • The third declaration statement declares three ‘ CHARACTER ’ string variables of length six characters. • The fourth declaration declares a ‘ CHARACTER ’ string parameter. The use of the asterisk denotes that the length of the string parameter will be determined by the compiler, this basically boils down to you not having to count the letters and explicitly supply the number. • The fifth and final declaration is also a ‘ CHARACTER ’ string parameter but the length of the string is specified as five and the length not assumed by the compiler (note an asterisk could have also been used here). 1.2 Concatenation & substrings In Fortran there is only ‘one’ intrinsic operator for the ‘ CHARACTER ’ type and it is dyadic (requires two operands). The operator is the ‘concatenation’ operator ‘ // ’ (two front slashes) and is used to append one ‘ CHARACTER ’ type to another ie. "abc"//"def" takes the string "def" and appends it to the string "abc" to give a single string "abcdef" . This is made clear in the next example code. ‘Substrings’ can also be extracted from strings by indexing the strings through a pair of appended brackets. So consider the example declaration ‘four’ above for the string parameter ‘ headline ’. Then ‘ headline(5:10) ’ would reference the fifth to the tenth (inclusive) characters of the string which would be ‘ United ’. The colon can be seen as globally referencing all possible characters in the extent of the 1 string but is then limited by the integer delimiters ‘5’ and ‘10’. Consider the next bit of code, what dostring but is then limited by the integer delimiters ‘5’ and ‘10’....
View Full Document
This note was uploaded on 02/13/2012 for the course MATH 5806 taught by Professor Brooks during the Fall '10 term at Minnesota.
- Fall '10