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
Click For Summary
SUMMARY

This discussion focuses on constructing a long character string in Fortran using an array of characters. The user demonstrates how to concatenate individual characters stored in a one-dimensional array into a single string variable. The correct implementation involves initializing a character array and using a loop to assign each character to the target string. Additionally, the conversation extends to manipulating a two-dimensional character array and storing its elements into a one-dimensional array.

PREREQUISITES
  • Understanding of Fortran character data types
  • Familiarity with Fortran array manipulation
  • Knowledge of Fortran string concatenation techniques
  • Basic programming concepts such as loops and indexing
NEXT STEPS
  • Explore Fortran string manipulation functions
  • Learn about Fortran array reshaping techniques
  • Investigate Fortran's character substring operations
  • Study advanced Fortran data structures for multi-dimensional arrays
USEFUL FOR

This discussion is beneficial for Fortran developers, programmers working with character data, and anyone looking to enhance their skills in string manipulation and array handling in Fortran.

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
 
Technology 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
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K