Fortran: How to make a long character made with an array

  • Context: Fortran 
  • Thread starter Thread starter hodei_cloud
  • Start date Start date
  • Tags Tags
    Array Fortran
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
hodei_cloud
Messages
19
Reaction score
0
Lets see if I explain myself. I want to FILL a string with the components of a character array. I have no idea, but seeing this example:
Code:
character(len=*),parameter::fname="     Paul",lname="Scholes"
character(len=20)::fullname

fullname=fname//" "//lname
I try this one

Code:
character(len=1),dimension(5)::x
character(len=100)::string
x(1)='h'
x(2)='e'
x(3)='l'
x(4)='l'
x(5)='o'
do i=1,5
  a=a//x(i)
enddo
I know that its wrong, but I don't know how to make it...
Any help will be gratefull
 
Physics news on Phys.org
Code:
program atc
    character(len=1), dimension(5)::X
    character(len=20)::a
    
    a = ""
    x(1) = 'h'
    x(2) = 'e'
    x(3) = 'l'
    x(4) = 'l'
    x(5) = 'o'

    do i = 1, 5
        a(i:i) = x(i)
    end do
    
    write(*,*) 'a = ', a

end program atc
 
  • Like
Likes   Reactions: 1 person
gsal said:
Code:
program atc
    character(len=1), dimension(5)::X
    character(len=20)::a
    
    a = ""
    x(1) = 'h'
    x(2) = 'e'
    x(3) = 'l'
    x(4) = 'l'
    x(5) = 'o'

    do i = 1, 5
        a(i:i) = x(i)
    end do
    
    write(*,*) 'a = ', a

end program atc

Thank you!
And if I want to do the same thing but with "x" being a matrix and "a" a array?
I try some programms, but no luck
 
If you have
Code:
character(len=20), dimension(5) :: a
then
Code:
a(i)
is an element of the array, and
Code:
a(i)(j:k)
is a substring of the element.
Code:
character(len=1), dimension(5,5) :: x = reshape( (/(char(i+ichar('a')),i = 0,24)/), &
& shape(x) )          ! sets x(1,1) = 'a', a(2,1) = 'b',  ..., x(5,5) = 'y'
character(len=20), dimension(5) :: a = (/ (' ', i = 1,5) /)

do i = 1,5
   do j = 1,5
       a(i)(j:j) = x(j,i)
   end do
end do

do i = 1,5
   write(*,*) 'a(',i,') = ',a(i)
end do
end
 
Last edited:
  • Like
Likes   Reactions: 1 person
Thanks, the array of a character was driving me crazy... haha