Also, is there a way in Fortran 90 to convert
an uppercase character to lower case, and vice-versa?
Vivek Rao
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
In VMS, use STR$CASE_BLIND_COMPARE
>Also, is there a way in Fortran 90 to convert
>an uppercase character to lower case, and vice-versa?
Call the C routines toupper and tolower
--
Ivan Reid, Paul Scherrer Institute, CH. re...@psi.ch
Support Jayne Hitchcock: http://www.geocities.com/hollywood/6172/helpjane.htm
Yes, it is. The trick is to compare the ASCII codes character by
character. There are three possibilities:
(i) The ASCII codes are equal. ---> The characters are equal.
(ii) The ASCII codes differ by 32, and one of them is at least 65
and at most 90. ---> The characters are letters, one is upper-
case and the other is lower-case.
(iii) None of the above. ---> The characters are not equal.
I enclose a piece of sample code. My apologies for the necessary
reformatting of the code.
PROGRAM ignore_case
IMPLICIT NONE
INTEGER :: i, identical
CHARACTER (LEN=80) :: string_1, string_2
WRITE (*, FMT='(A)', ADVANCE='NO') 'Please give string # 1: '
READ (*, FMT='(A)') string_1
WRITE (*, FMT='(A)', ADVANCE='NO') 'Please give string # 2: '
READ (*, FMT='(A)') string_2
i = identical(string_1, string_2)
IF ( i .EQ. 0) THEN
WRITE (*, FMT='(A)') 'The two strings are of same length and
identical.'
ELSE IF ( i .GT. 0) THEN
WRITE (*, FMT='(A,I4)') 'The two strings are identical up to
character # ', i - 1
END IF
END PROGRAM ignore_case
INTEGER FUNCTION identical(string_a, string_b)
IMPLICIT NONE
INTEGER :: i, max_length
CHARACTER (LEN=*), INTENT(IN) :: string_a, string_b
identical = 0
IF ( LEN (string_a) .NE. LEN (string_b) ) identical = 1
IF ( LEN_TRIM(string_a) .NE. LEN_TRIM(string_b) ) identical = 1
IF ( identical .EQ. 0 ) max_length = LEN_TRIM(string_a)
IF ( identical .EQ. 1 ) max_length = MIN(LEN(string_a), &
LEN(string_b), &
INDEX(string_a, ' '), &
INDEX(string_b, ' '))
DO i = 1, max_length
IF &
((IACHAR(string_a(i:i)) .NE. IACHAR(string_b(i:i))) .AND. &
(ABS(IACHAR(string_a(i:i))- IACHAR(string_b(i:i))) .NE. 32 &
.OR. MIN(IACHAR(string_a(i:i)), IACHAR(string_b(i:i))) .LT. 64 &
.OR. MIN(IACHAR(string_a(i:i)), IACHAR(string_b(i:i))) .GT. 91))&
identical = i
END DO
END FUNCTION identical
> Also, is there a way in Fortran 90 to convert
> an uppercase character to lower case, and vice-versa?
Same procedure. Use
uppercase = ACHAR(IACHAR(lowercase)-32)
lowercase = ACHAR(IACHAR(uppercase)+32)
after you have checked that the ASCII codes are within the limits.
** Please note that the intrinsic functions IACHAR and ACHAR were
** used rather than ICHAR and CHAR. ICHAR and CHAR rely on system-
** dependent sorting, whereas IACHAR and ACHAR use ASCII.
Happy programming
ulf
--
Ulf Mazurek e-mail: u...@www.chem.tu-berlin.de
Inst. Organ. Chemie, Sekr. C4
Technische Universitaet Berlin
Strasse des 17. Juni 135
D-10623 Berlin
Germany
Usual disclaimers apply.
2 + 2 = 5 for extremely large values of 2.
Here is a simple module containing a function that converts a string to
upper case (using the ASCII character codes), and a function that uses the
first function to compare two strings, ignoring case.
module string_mod
contains
function upcase(string)
character(*),intent(in) :: string
character(len(string)) :: upcase
integer :: i,icode
do i=1,len(string)
icode = iachar(string(i:i))
if (icode >= 97 .and. icode <= 122) then ; upcase(i:i) = achar(icode-32)
else ; upcase(i:i) = achar(icode) ; end if
end do
return
end function upcase
logical function equal_nocase(string1,string2)
character(*),intent(in) :: string1,string2
equal_nocase = upcase(string1) == upcase(string2)
return
end function equal_nocase
end module string_mod
--
Ronald Sverdlove Computing Systems Research
r...@sarnoff.com Sarnoff Corporation
Tel. 609-734-2517 CN 5300
FAX 609-734-2662 Princeton, NJ 08543-5300