Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

I/O formatting case insensitive reading

h_amini
Beginner
634 Views

My program reads a data file to find a word to read the data afterwards. The thing is the data file is case insensitive opposite to the reading statement. Cold someone let me know how I can solve the problem.

As an example the following code echoes hi only if lower cases are inputted.

character*2, a

read*, a

if (a .eq. "hi") print*,"hi"

END

Many thanks

Hamid

0 Kudos
3 Replies
Les_Neilson
Valued Contributor II
634 Views

For ASCII text ("Latin" characters a-z, A-Z) it is ok; once you move to other languages thoughmore work is needed.

integer :: ilen
integer :: ich

ilen = len_trim(string)

do i = 1, ilen
ich = ichar(string(i:i) ! Getposition of string(i:i) in ASCII set
if (ich > 96 && ich < 123) then ! If inthe range a to z
ich = ich - 32 ! put it in range A to Z
string(i:i) = char(ich) ! put the new character back in string(i:i)
endif
enddo

Les
0 Kudos
Steven_L_Intel1
Employee
634 Views
The usual way of dealing with this is to convert the read string to lowercase and compare to the downcased result. Module USER32 defines a Win32 API function CharLower which does this, but it wants a null-terminated string. It might make sense to write your own - it isn't hard.

There's also a lstrcmpi function which is a case-blind compare. You could use it like this:

[plain]use kernel32
character(2) a
read (*,'(a)') a
if (lstrcmpi(a // char(0), 'hi'C) == 0) print *, 'hi'
end[/plain]
Note that I have used an explicit A format to do the read. You should not use list-directed input for this purpose - it may not do what you want. You might write your own case-blind compare function that wraps lstrcmpi.
0 Kudos
h_amini
Beginner
634 Views

A big thank you to Les and Steve for all their helpful comments. Both of the mentioned solutions work perfectly.

0 Kudos
Reply