For Programmers: Free Programming Magazines  


Home > Archive > Fortran > January 2006 > How to compare two strings in 77?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to compare two strings in 77?
jane.sync@yahoo.com

2006-01-29, 6:59 pm

I thought I could test two strings with the code below, but it doesn't
work. I'm passing a string into the function below, but the result is
always 'not equal'. How are strings compared in Fortran 77? Thanks.

function evaluateinput(str1) result(string)
implicit none
character(12) str1
character(12) string
if (str1 .EQ. 'test') then
string = 'equal'
else
string = 'not equal'
endif
return
end

jane.sync@yahoo.com

2006-01-29, 6:59 pm

I think I got it. Does the following code look like something a Fortran
programmer would use to test two strings?

function evaluateinput(str1) result(string)
implicit none
character(12) str1
character(12) string
if (LLT(str1,'test')) then
string = 'less than'
else if (LGT(str1,'test')) then
string = 'greater than'
else
string = 'equal'
end if
return
end

Rich Townsend

2006-01-29, 9:56 pm

jane.sync@yahoo.com wrote:
> I think I got it. Does the following code look like something a Fortran
> programmer would use to test two strings?
>
> function evaluateinput(str1) result(string)
> implicit none
> character(12) str1
> character(12) string
> if (LLT(str1,'test')) then
> string = 'less than'
> else if (LGT(str1,'test')) then
> string = 'greater than'
> else
> string = 'equal'
> end if
> return
> end
>


I think you've found a work-around, not a fix. The problem is the declaration of
str1. If you pass a string of length shoother than 12, (say, 5), then the
characters of str1 from position 6 onwards will contain garbage. It is this
garbage that prevents str1 from being equal to 'test'; but the collating
operators (LLT and LGT) will still work.

The fix? Replace

character(12) str1

by

character(*) strl

cheers,

Rich
jane.sync@yahoo.com

2006-01-29, 9:56 pm

Ahhh. Very interesting. Thanks!

David Flower

2006-01-30, 8:00 am


jane.sync@yahoo.com wrote:
> I thought I could test two strings with the code below, but it doesn't
> work. I'm passing a string into the function below, but the result is
> always 'not equal'. How are strings compared in Fortran 77? Thanks.
>
> function evaluateinput(str1) result(string)
> implicit none
> character(12) str1
> character(12) string
> if (str1 .EQ. 'test') then
> string = 'equal'
> else
> string = 'not equal'
> endif
> return
> end


Well, certainly not in the above code, which is not FORTRAN 77. Try

CHARACTER*9 FUNCTION string ( str1 )
IMPLICIT NONE
CHARACTER*(*) str1
IF ( str1 .EQ. 'test' ) THEN
string = 'equal'
ELSE
string = 'not equal'
END IF
RETURN
END

Dave Flower

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com