title: Character strings in Fortran: Portable LEN_TRIM and LNBLNK?
body: <p><strong>I need a portable function/subroutine to locate the position of the last non-blank character in a string</strong>.  I've found two options:  <code>LEN_TRIM</code> and <code>LNBLNK</code>.  However, different compilers seem to have different standards.  The official documentation for the following compilers suggests that LEN_TRIM is part of the Fortran 95 standard on the following platforms:</p>
  
  <ul>
  <li>IBM:  <a href="http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.xlf111.cell.doc/xlflr/lentrim.htm" rel="nofollow noreferrer">LEN_TRIM</a></li>
  <li>Intel: <a href="http://www.intel.com/software/products/compilers/docs/flin/main_for/lref_for/source_files/rflnblnk.htm" rel="nofollow noreferrer">LNBLNK</a> and <a href="http://www.intel.com/software/products/compilers/docs/flin/main_for/lref_for/source_files/rflentr.htm" rel="nofollow noreferrer">LEN_TRIM</a></li>
  <li>gfortran: <a href="http://gcc.gnu.org/onlinedocs/gfortran/LNBLNK.html" rel="nofollow noreferrer">LNBLNK</a> and <a href="http://gcc.gnu.org/onlinedocs/gfortran/LEN_005fTRIM.html#LEN_005fTRIM" rel="nofollow noreferrer">LEN_TRIM</a></li>
  <li>PGI: <a href="http://www.pgroup.com/doc/pghpf_ref/hpfwsr08.htm#Heading257" rel="nofollow noreferrer">LEN_TRIM</a></li>
  </ul>
  
  <p><strong>However, it appears that nothing is guaranteed on compilers released before the F95 standard.</strong>  My experience has been that older compilers might specify either <code>LEN_TRIM</code> or <code>LNBLNK</code>, but not necessarily both.  My solution has been to use preprocessor conditionals:</p>
  
  <pre><code>#ifdef USE_LEN_TRIM
          iNameLen = LEN_TRIM(cabase)
          iExtLen = LEN_TRIM(caext)
  #else
          iNameLen = LNBLNK(cabase)
          iExtLen = LNBLNK(caext)
  #endif
  </code></pre>
  
  <p>and then pass <code>-DUSE_LEN_TRIM</code> to the preprocessor.  However, I am not a big fan of preprocessor conditionals &amp; extra compile-time flags.  <strong>Do you have any suggestions for a portable (before the Fortran 95 standard) function that locate the position of the last non-blank character in a string?</strong></p>