Fortran: creating array containing characters

  • Context: Fortran 
  • Thread starter Thread starter _Andreas
  • Start date Start date
  • Tags Tags
    Array Fortran
Click For Summary
SUMMARY

This discussion focuses on creating an array of characters in Fortran, specifically using a 1-dimensional array to store individual characters from a string. The user initially attempted to define a character array with a fixed dimension but encountered issues. The solution provided involves declaring a character string and then populating a character array using a loop to extract each character from the string.

PREREQUISITES
  • Understanding of Fortran syntax and data types
  • Familiarity with character strings in Fortran
  • Basic knowledge of loops in programming
  • Experience with array manipulation in Fortran
NEXT STEPS
  • Explore Fortran character array manipulation techniques
  • Learn about Fortran string handling functions
  • Investigate advanced array operations in Fortran
  • Study Fortran's memory management for dynamic arrays
USEFUL FOR

This discussion is beneficial for Fortran programmers, particularly those working with string manipulation and character arrays, as well as students learning the fundamentals of Fortran programming.

_Andreas
Messages
141
Reaction score
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
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   Reactions: camila

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 18 ·
Replies
18
Views
7K
Replies
7
Views
3K
  • · Replies 11 ·
Replies
11
Views
12K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 13 ·
Replies
13
Views
4K