forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Converting string into array of characters

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
Zach



Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

PostPosted: Mon May 15, 2023 11:19 am    Post subject: Converting string into array of characters Reply with quote

How can I
1. convert a string into array of characters?
1. convert an array of characters into a string?
Patrick.
Back to top
View user's profile Send private message Send e-mail
Kenneth_Smith



Joined: 18 May 2012
Posts: 696
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Mon May 15, 2023 12:11 pm    Post subject: Reply with quote

Code:
implicit none
integer i, n
character(len=26) :: a2z, z2a
character(len=1)  :: ch(26)
n   = 26
a2z = ''
print'(A)', a2z
do i = 97, 97+n, 1
  a2z = trim(a2z)//char(i)
end do
print'(A)', a2z    ! Initial string
do i = 1, n
  ch(i) = a2z(i:i) ! Character array
end do
z2a = ''
print'(A)', z2a
do i = n, 1, -1    !Reverse order
  z2a = trim(z2a)//ch(i)
end do
print'(A)', z2a   ! New string formed from character array
end


Code:
abcdefghijklmnopqrstuvwxyz

zyxwvutsrqponmlkjihgfedcba
Back to top
View user's profile Send private message Visit poster's website
Kenneth_Smith



Joined: 18 May 2012
Posts: 696
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Mon May 15, 2023 5:20 pm    Post subject: Reply with quote

Expanding to use subroutines "String2array" and "Array2string":

Code:
implicit none
integer i, n
character(len=128) :: a2z, a2zcopy
character(len=1), allocatable  :: array(:)

interface

  subroutine string2array(str,array)
    character(len=*), intent(in) :: str
    character(len=1), allocatable, intent(out) :: array(:)
  end subroutine string2array

  subroutine array2string(array,str)
    character(len=1), allocatable, intent(in) :: array(:)
    character(len=*), intent(out) :: str
  end subroutine
 
end interface

n   = 26
a2z = ''
print'(A)', trim(a2z)
do i = 97,97+n-1
  a2z = trim(a2z)//char(i)
end do

print'(25A,5X,A)', 'Initial string:          ',trim(a2z)

call string2array(a2z,array)

a2zcopy = ''
print'(A)', trim(a2zcopy)

call array2string(array,a2zcopy)
print'(25A,5X,A)', 'String from char array:  ',trim(a2zcopy)
end

  subroutine string2array(str,array)
  character(len=*), intent(in) :: str
  character(len=1), allocatable, intent(out) :: array(:)
  integer i, n
  if (allocated(array)) deallocate(array)
    n = len_trim(str)
    allocate(array(n))
    forall (i=1:n) array(i) = str(i:i)
  end subroutine string2array

  subroutine array2string(array,str)
  character(len=1), allocatable, intent(in) :: array(:)
  character(len=*), intent(out) :: str
  integer i, n
  if (.not. allocated(array)) STOP 'Error'
    str = ''
    do i = 1, size(array), 1
      str = trim(str)//array(i)
    end do
  end subroutine array2string
Back to top
View user's profile Send private message Visit poster's website
Zach



Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

PostPosted: Tue May 16, 2023 10:54 am    Post subject: Reply with quote

Thank you very much for the code with which you responded to my query! Patrick.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group