swapcase Function

private elemental function swapcase(self)

Return a copy of the string with uppercase characters converted to lowercase and vice versa.

 type(string) :: astring
 logical      :: test_passed(1)
 astring = '  Hello World!   '
 test_passed(1) = astring%swapcase()//''=='  hELLO wORLD!   '
 print '(L1)', all(test_passed)

Type Bound

string

Arguments

Type IntentOptional Attributes Name
class(string), intent(in) :: self

The string.

Return Value type(string)

Upper case string.


Contents

Source Code


Source Code

   elemental function swapcase(self)
   !< Return a copy of the string with uppercase characters converted to lowercase and vice versa.
   !<
   !<```fortran
   !< type(string) :: astring
   !< logical      :: test_passed(1)
   !< astring = '  Hello World!   '
   !< test_passed(1) = astring%swapcase()//''=='  hELLO wORLD!   '
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string), intent(in) :: self     !< The string.
   type(string)              :: swapcase !< Upper case string.
   integer                   :: n1       !< Characters counter.
   integer                   :: n2       !< Characters counter.

   if (allocated(self%raw)) then
      swapcase = self
      do n1=1, len(self%raw)
         n2 = index(UPPER_ALPHABET, self%raw(n1:n1))
         if (n2>0) then
            swapcase%raw(n1:n1) = LOWER_ALPHABET(n2:n2)
         else
            n2 = index(LOWER_ALPHABET, self%raw(n1:n1))
            if (n2>0) swapcase%raw(n1:n1) = UPPER_ALPHABET(n2:n2)
         endif
      enddo
   endif
   endfunction swapcase