- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
A big thank you to Les and Steve for all their helpful comments. Both of the mentioned solutions work perfectly.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page