Fortran: creating array containing characters

In summary, the conversation discusses creating an array of characters, with one person expressing confusion on how to do it and suggesting a method that did not work. Another person then provides a solution using a character string and a loop to create the desired array.
  • #1
_Andreas
144
1
Hello.

I'd like to create an array containing characters (basically a word stored as a 1xn array). I have no idea how to do this, however. I thought that the most obvious way to do it (yes, it would've been highly impractical for long words) was to write

character, dimension(3) :: A

A=[F,P,G]

but that doesn't work at all. Any suggestions?
 
Technology news on Phys.org
  • #2
Ok, problem solved. If anyone's interested, this is how you do it:

character(len=n) :: string
character, dimension(n) :: array
integer :: i

string="blablabla and so on"

do i=1,len(string)
array(i)=string(i:i)
end do
 
  • Like
Likes camila

1. How do I create an array containing characters in Fortran?

To create an array containing characters in Fortran, you can use the CHARACTER data type and the DIMENSION statement. The DIMENSION statement specifies the size of the array, and the CHARACTER data type allows you to store characters in each element of the array.

2. What is the syntax for creating an array of characters in Fortran?

The syntax for creating an array of characters in Fortran is: CHARACTER (length) :: array_name (size). The length specifies the maximum number of characters that can be stored in each element, and the size specifies the number of elements in the array.

3. Can I initialize an array of characters in Fortran?

Yes, you can initialize an array of characters in Fortran using the DATA statement. The DATA statement allows you to specify the initial values for each element of the array. For example, DATA array_name / 'A', 'B', 'C' / will initialize the first three elements of the array with the characters 'A', 'B', and 'C'.

4. How do I access and modify elements in an array of characters in Fortran?

To access and modify elements in an array of characters in Fortran, you can use the index notation. For example, to access the first element of the array, you would use array_name(1). To modify the value of an element, you can simply assign a new character to it. For example, array_name(2) = 'D' will change the second element of the array to the character 'D'.

5. Can I use loops to work with arrays of characters in Fortran?

Yes, you can use loops to work with arrays of characters in Fortran. Loops allow you to perform the same operation on each element of the array. For example, you can use a DO loop to initialize all elements of the array to a specific character, or to print out the values of each element in the array.

Similar threads

  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
18
Views
6K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
11
Views
11K
Back
Top