title: Trouble with if/then statements containing character variables in Fortran 77
body: <p>I am currently trying to write a program in Fortran 77 that calculates the number of words in a text file. The text files looks like this:</p>
  
  <p>Hello world. Hello world. Hello world. Hello world. Hello world. Hello world. Hello world.       Hello world. Hello world. Hello world. Hello world. Hello world. Hello world.</p>
  
  <p>My program currently looks like this:</p>
  
  <pre><code>       program COUNT
         implicit none
  
         character text*100000
         integer i, a, nw
         nw=1 !number of words                                                     
  
         open(9, FILE='file.txt', STATUS='old')
         read(9, '(A)') text
  
         a=0
    10   do i=1, LEN_TRIM(text)
            if (text(i:i) .ne. " ") then
               if (a .eq. 0) then
                  goto 10
               else
                  a=0
                  nw=nw+1
                  goto 10
               endif
            else
               if (a .eq. 0) then
                  a=a+1
                  goto 10
               else
                  goto 10
               endif
            endif
         enddo
  
         print *, "Number of words: ", nw
  
         end
  </code></pre>
  
  <p>I did this on paper and it should work, however, my program is getting stuck in the do-loop. I think that this might have something to do with this statement:</p>
  
  <pre><code>       if (text(i:i) .ne. " ") then
  </code></pre>
  
  <p>Am I allowed to write the logical expression this way? If not, does anyone have any hints on how I could rewrite this code? I'm sorry if my program is a bit messy; I'm relatively new at this. I'm trying to improve my coding skills to do computational chemistry. Thanks so much for the help!</p>