Fortran: integer string example

I am trying to write simple Fortran code for practice. It is supposed to multiply the numbers in the range. Each time, the resulting product is converted to a string, because I want to see if it consists of the same numbers.

I checked the way to convert an integer to a string and typed the components of the string, and everything went right. Then I need to compare the components of the string for which I use the string (number: number). But I could not get the code to do it right.

Here's the code and output:

program test implicit none character(10) myString character(1) a,b,c,d,e,f integer::i,j,k do i=900,901,1 j=900 k=i*j write(*,*)'k =', k write(myString,'(i10)') k write(*,*)'myString = ', myString a=myString(1:1) b=myString(2:2) c=myString(3:3) d=myString(4:4) e=myString(5:5) f=myString(6:6) print*,a,b,c,d,e,f if (d==f) then print*,'hobla' else print*,'fobla' end if end do stop end program test 

So, I defined the characters: a, b, c, d, e, f to contain the components of the string. And used myString (i: i) to find each component and store it in one of the characters a, b, c, d, e, f. But it seems that only the first two are working correctly, the rest are not saved!

Output:

  k = 810000 myString = 810000 81 fobla k = 810900 myString = 810900 81 fobla 

Notice 81. It was supposed that he should give 810,000 for the first time and type β€œhobla”. And give 810900 a second time and type "fobla." But this did not happen!

Can someone show me how to allow myString to accept zeros as characters?

0
source share
2 answers

Ok, so I understood the problem. Consider the following changes to your code:

 program test implicit none character(10) myString character(1) a,b,c,d,e,f integer::i,j,k do i=900,901,1 j=900 k=i*j write(*,*)'k =', k write(myString,'(i10)') k write(*,*)'myString = ', myString a=myString(1:1) b=myString(2:2) c=myString(3:3) d=myString(4:4) e=myString(5:5) f=myString(6:6) write(*,*) a write(*,*) b write(*,*) c write(*,*) d write(*,*) e write(*,*) f if (d==f) then print*,'hobla' else print*,'fobla' end if end do stop end program test 

Resulting result:

  k = 810000 myString = 810000 8 1 fobla k = 810900 myString = 810900 8 1 fobla 

This is because format I aligns numbers correctly when printed. So there is a bunch of leading white spaces, because your number is only 6 digits, and your line you write is 10. So you get 4 leading spaces.

If you change the format string so that you have:

 write(myString,'(i10.10)') k 

and then instead of filling with spaces, 0. will be placed. Thus, you can always compare numbers if you want.

+2
source

This statement

 write(myString,'(i10)') k 

writes the value of k to a field with 10 characters. Since k has only 6 significant digits, the first 4 characters in myString are filled with spaces. Then you assign the first 6 characters of myString (i.e. 4 spaces and the numbers 8 and 1) to the variables a,b,c,d,e,f and print them - 4 spaces and 2 digits.

Try printing other characters at positions 7..10 myString and you should see your "missing" numbers.

+4
source

Source: https://habr.com/ru/post/970423/


All Articles