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

  • Thread starter Thread starter Taylor_1989
  • Start date Start date
  • Tags Tags
    Fortran String
AI Thread Summary
The discussion revolves around coding a Vigenère cipher in Fortran, focusing on issues with string manipulation and output formatting. The user initially encounters a problem with the `repeat` function, where the key 'maths' is repeated but results in an output that exceeds the allocated space for the variable. The solution involves adjusting the key's length to avoid trailing blank spaces. Subsequently, the user aims to align the key's length with the plaintext for proper ciphering. The output format is also a concern, as the user wants to print the results horizontally instead of vertically. A suggested fix includes using `repeat(trim(key), 256)` to ensure the key is repeated correctly and matches the plaintext length. After implementing these changes, the user confirms success in achieving the desired output format. The discussion highlights the importance of string length management and output formatting in programming.
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
 
Technology 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)
 
  • #10
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]
 

Similar threads

Replies
6
Views
3K
Replies
8
Views
2K
Replies
59
Views
11K
Replies
4
Views
2K
Replies
10
Views
3K
Replies
11
Views
11K
Back
Top