Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. May 23, 2014 #1
    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 (Text):

    character(len=*),parameter::fname="     Paul",lname="Scholes"
    character(len=20)::fullname

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

    Code (Text):

    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 dont know how to make it...
    Any help will be gratefull
     
  2. jcsd
  3. May 23, 2014 #2
    Code (Text):

    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
     
     
  4. May 24, 2014 #3
    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
     
  5. May 24, 2014 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    If you have
    Code (Text):

    character(len=20), dimension(5) :: a
     
    then
    Code (Text):
    a(i)
    is an element of the array, and
    Code (Text):
    a(i)(j:k)
    is a substring of the element.
    Code (Text):

    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: May 24, 2014
  6. May 24, 2014 #5
    Thanks, the array of a character was driving me crazy... haha
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook