Skip to content

Instantly share code, notes, and snippets.

@natebenes
Created April 21, 2010 19:17
Show Gist options
  • Save natebenes/374279 to your computer and use it in GitHub Desktop.
Save natebenes/374279 to your computer and use it in GitHub Desktop.
logical function validExp(inputstring, length)
IMPLICIT NONE
character(len=500), intent(in) :: inputstring
integer, intent(in) :: length
integer:: i, counter = 0
character(len=1) :: testCharacter
character(len=16), parameter :: allowed = "1234567890+-/$()"
validExp = .FALSE.
DO i = 1, length
testCharacter = inputstring(i:i)
if((i==1) .AND. (testCharacter /= "(")) exit
if(index(testCharacter, allowed) == 0) exit
! Hint: if at any point, there are more right parentheses
! than left parentheses, we have an issue.
if(testCharacter=="(") counter = counter + 1
if(testCharacter==")") counter = counter - 1
if((i == length) .AND. (counter == 0)) validExp = .TRUE.
END DO
END FUNCTION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment