Tuesday, May 13, 2014

fortran learning-how to convert a number to a character and vice-versa.

1. Character initialization
character*10 string, format
CHARACTER :: char1,char2,letter="I"
CHARACTER, PARAMETER :: a="a",b="b",c="c"
CHARACTER (LEN=6) :: class1=’MT3501’, class2=’MT3502’
,class3=’MT3503’
CHARACTER (LEN=*), PARAMETER :: headline="Man United will win the league?"
CHARACTER (LEN=5), PARAMETER :: name="Steve"

2. how to convert a number to a character and vice-versa.

program characterconversions

character*50 s
integer i
double precision d
logical t,f

s = '1234' 
read (s,*) i

s = '1234.5678'
read (s,*) d

s = 'T' !TRUE
read (s,*) t

s = 'F' !FALSE
read (s,*) f

print *,i
print *,d
print *,t
print *,f

write (s, *) i
print *,s

write (s, *) d
print *,s

write (s, *) t
print *,s

write (s, *) f
print *,s

print '("Press any key to exit... "$)'
read (*,*)

end

THE OUTPUT IS:

1234
1234.5678
T
F
1234
1234.5678
T
F
Press any key to exit...

3. How to operate the characters and strings.

PROGRAM characters
IMPLICIT NONE
CHARACTER (LEN=*), PARAMETER :: headline="Man United will win the league?"
CHARACTER (LEN=*), PARAMETER :: fname="Steve", lname="Smith"
CHARACTER (LEN=11) :: fullname ! *** Example of concatenation of two strings ***
fullname=fname//lname
PRINT*,fullname !*** Concatenation of a string a character and a string ***
fullname=fname//" "//lname
PRINT*,fullname ! *** Example of a substring ***
PRINT*,headline(5:10)
end program

No comments:

Post a Comment