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

In summary, the conversation discusses how to fill a string with the components of a character array using an example. The individual is struggling to understand how to accomplish this task and seeks help. The conversation also mentions using a matrix for the character array and provides a solution using nested loops.
  • #1
hodei_cloud
19
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
 
Technology news on Phys.org
  • #2
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 1 person
  • #3
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
 
  • #4
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 1 person
  • #5
Thanks, the array of a character was driving me crazy... haha
 

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

What is Fortran?

Fortran is a high-level programming language used for scientific and numerical computation. It was first created in the 1950s and has since been updated and improved upon.

How do I create a long character in Fortran using an array?

To create a long character using an array in Fortran, you can use the CHARACTER data type. This allows you to declare a string of characters of any length, which can be used as an array to create a longer character.

What is the syntax for creating a long character using an array in Fortran?

The syntax for creating a long character using an array in Fortran is as follows: CHARACTER(len) :: long_char where len is the desired length of the character and long_char is the name of the character variable.

Can I use a loop to fill the array with characters in Fortran?

Yes, you can use a loop to fill the array with characters in Fortran. You can use a DO loop to iterate through the array and assign each character to the corresponding element.

How can I access and manipulate individual characters in a long character array in Fortran?

You can access and manipulate individual characters in a long character array in Fortran by using the array indexing notation. For example, long_char(1) would give you the first character in the array. You can then use this notation to assign new characters or modify existing ones.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top