Repeating Strings in Fortran: Why is My Output Not Matching My Key?

  • Topic: Fortran 
  • Thread starter Thread starter Taylor_1989
  • Start date Start date
  • Tags Tags
    Fortran String
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
9 replies · 4K views
Taylor_1989
Messages
400
Reaction score
14
Hi, guys. For my project this week I have to code a cipher, and I am just getting used to what, what with the code. The problem I am having is that I have assinged my key and instered that into the repeat function but I only get one output of maths and not seven, why is this?

Fortran:
program Jason

implicit none

integer:: j
character*256 :: string, key, i

string='Hello all my name is Jason'
key='maths '

i=repeat(key,7)

write(*,*),iend program jason
 
Physics news on Phys.org
key has slots up to 256 characters, when you concatenates key 7 times using repeat you will generate 256*7 slots. You store this new string in i which is a 256 long character variable - i is running out of space. You can solve it by declaring key as, e.g. a 6 slot character or anything whose seven multiple is less than 256.
 
The variable key is always 256 characters long, so it consists of 'maths' followed by 251 blank characters. Repeating that 7 times doesn't fit in variable i.

Either make key shorter or use trim().

By the way, there is an unnecessary comma after write(*,*) and I strongly recommend you not using i as a variable for anything other than an integer.
 
Thanks for the response guys, I have corrected that code and now want to to get math to repeat to my plaintext, so but the issue is, it is only outputting to my hello and not the the rest. Can someone please advise, my code is below:

Fortran:
program Jason

implicit none

integer:: i,y,z,j
character*256 :: plaintext, ciphered, key 
plaintext='Hello all my name is Jason'
key='maths'
   do i= 1, len_trim(plaintext), 1
     
       write(*,*) plaintext(i:i), key(i:i)
     
   end do

 
end program jason

ignor the extra intgers that was me applying different methods to loop to see if I could get it to work. Also how would I get the to be printed horizontal and not vertical. I have a fortran book I use but not a lot of information for me to learn from.
 
Last edited by a moderator:
It's hard to tell what you're trying to do. You have a loop that will run 26 times, once for each character in the string plaintext. In the loop body the write statement outputs one character from plaintext and one character from key. The first three iterations of the loop will display something like this:
Code:
H    m
e    a
l     t
and so on
After each execution of the write statement, a newline character is also printed, so that the next time write is executed, it will be on the next line.

A big problem I see in your code is that your loop is set up to print each character in the plaintext string, but the maths string has only five characters, so you're attempting to print characters in the maths string beyond its end. It's been a while since I've written any Fortran code, and I don't have a compiler, so I'm not sure what your program will produce once it gets past the end of the shorter string.
 
Mark44 said:
I'm not sure what your program will produce once it gets past the end of the shorter string.
It will be like

Code:
Hm
ea
lt
lh
os

a
l
l
and so on since key actually has trailing 251 blank spaces.
 
blue_leaf77 said:
since key actually has trailing 251 blank spaces.
Yep. I forgot that key was declared to hold 256 characters.
 
blue_leaf77 said:
It will be like

Code:
Hm
ea
lt
lh
os

a
l
l
and so on since key actually has trailing 251 blank spaces.

I have to create a Vigenère cipher so I am trying to get my key to be the same length as my plaintext so I can assing the numbers and the shift ect. I am trying the method that was mentioned to me in a perivious post about a waterfall approach, I drew out what I had to do, then I have broken each part down, but it this part that I can't seem to get right at the moment. The outupt I would like it this:'Hello all my name is Jason'
mathsmathsmathsmathsm

so the once the letter have been connected up I can move on to the next part and start assing and shifting the numbers.
 
Taylor_1989 said:
I have to create a Vigenère cipher so I am trying to get my key to be the same length as my plaintext so I can assing the numbers and the shift ect. I am trying the method that was mentioned to me in a perivious post about a waterfall approach, I drew out what I had to do, then I have broken each part down, but it this part that I can't seem to get right at the moment. The outupt I would like it this:'Hello all my name is Jason'
mathsmathsmathsmathsm

so the once the letter have been connected up I can move on to the next part and start assing and shifting the numbers.
In the code in your first post, try replacing
Code:
i=repeat(key,7)
with
Code:
i=repeat(key(1:5),7)
 
I have done it! finally persistence pays off. So just to make sure my code now look like this

Code:
program Jason

implicit none

integer:: i,z,j
character*256 :: plaintext, ciphered,d,y
character*5::keyplaintext='Hello all my name is Jason'
key='maths'
y=repeat(trim(key),256)

   do i= 1, len_trim(plaintext), 1
       
       write(*,*) plaintext(i:i),y(i:i)
   
   end doend program jason

[\code]