Manual Reference Pages  - switch (3m_strings)

NAME

switch(3f) - [M_strings:ARRAY] converts between CHARACTER scalar and array of single characters (LICENSE:PD)

CONTENTS

Synopsis
Description
Examples
Author
License

SYNOPSIS

pure function switch(array) result (string)

    character(len=1),intent(in) :: array(:)
    character(len=SIZE(array))  :: string

or

pure function switch(string) result (array)

    character(len=*),intent(in) :: string
    character(len=1)            :: array(len(string))

DESCRIPTION

SWITCH(3f): generic function that switches CHARACTER string to an array of single characters or an array of single characters to a CHARACTER string. Useful in passing strings to C. New Fortran features may supersede these routines.

EXAMPLES

Sample program:

   program demo_switch
   use M_strings, only : switch, isalpha, islower, nospace
   character(len=*),parameter :: &
   & dashes=’-----------------------------------’
   character(len=*),parameter :: string=’This is a string’
   character(len=1024)        :: line

! First, examples of standard Fortran features ! returns array [F,T,T,T,T,T] write(*,*)[’A’,’=’,’=’,’=’,’=’,’=’] == ’=’ ! this would return T write(*,*)all([’=’,’=’,’=’,’=’,’=’,’=’] == ’=’) ! this would return F write(*,*)all([’A’,’=’,’=’,’=’,’=’,’=’] == ’=’)

! so to test if the string DASHES is all dashes ! using SWITCH(3f) is if(all(switch(dashes) == ’-’))then write(*,*)’DASHES is all dashes’ endif

! so to test is a string is all letters ! isalpha(3f) returns .true. only if character is a letter ! false because dashes are not a letter write(*,*) all(isalpha(switch(dashes))) ! false because of spaces write(*,*) all(isalpha(switch(string))) ! true because removed whitespace write(*,*) all(isalpha(switch(nospace(string))))

! to see if a string is all uppercase ! show the string write(*,*) string ! converted to character array write(*,’(1x,*("[",a,"]":))’) switch(string) write(*,’(*(l3))’) islower(switch(string))

! we need a string that is all letters line=nospace(string) write(*,*)’LINE=’,trim(line) ! all true except first character write(*,*) islower(switch(nospace(string))) ! should be false write(*,*) all(islower(switch(nospace(string)))) ! should be true write(*,*) all(islower(switch(nospace(string(2:)))))

end program demo_switch

Expected output

    > F T T T T T
    > T
    > F
    > DASHES is all dashes
    > F
    > F
    > T
    > This is a string
    > [T][h][i][s][ ][i][s][ ][a][ ][s][t][r][i][n][g]
    >  F  T  T  T  F  T  T  F  T  F  T  T  T  T  T  T
    > LINE=Thisisastring
    > F T T T T T T T T T T T T
    > F
    > T

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 switch (3m_strings) July 20, 2024
Generated by manServer 1.08 from 2e252268-0a78-40da-881b-45a81bea3b3b using man macros.