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

  • Context: Fortran 
  • Thread starter Thread starter Taylor_1989
  • Start date Start date
  • Tags Tags
    Fortran String
Click For Summary

Discussion Overview

The discussion revolves around coding a cipher in Fortran, specifically addressing issues with string repetition and output formatting. Participants explore the challenges of using the repeat function and the implications of character array sizes in the context of a Vigenère cipher implementation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant reports an issue with the repeat function, stating that they only receive one output instead of the expected seven when repeating a key.
  • Another participant suggests that the problem arises from the character length of the key, which is set to 256 characters, leading to insufficient space for the repeated string.
  • It is noted that the key contains trailing blank spaces, which may affect the output when attempting to print characters from both the plaintext and the key.
  • A participant expresses confusion about the loop structure, indicating that it prints characters from the plaintext and key vertically, rather than horizontally.
  • One participant proposes a solution to adjust the key length to match the plaintext length for proper cipher functionality.
  • Another participant shares a successful code modification that resolves the initial issues, demonstrating persistence in troubleshooting.

Areas of Agreement / Disagreement

Participants generally agree on the issues related to character lengths and the implications of trailing spaces in the key. However, there is no consensus on the best approach to achieve the desired output format and functionality for the cipher.

Contextual Notes

Limitations include the dependence on character array sizes and the potential for undefined behavior when accessing characters beyond the length of the shorter string. The discussion also reflects varying levels of familiarity with Fortran syntax and programming concepts.

Who May Find This Useful

Readers interested in Fortran programming, string manipulation, and cipher algorithms may find this discussion relevant, particularly those working on similar coding projects or learning about character arrays and loops in Fortran.

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 ·
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 11 ·
Replies
11
Views
11K