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

In summary, your code is trying to print characters in the maths string beyond its end, which will result in something like 'Hmealtlhosall' being output. You might be able to solve this by breaking the code down into smaller parts and making sure each part is working correctly.
  • #1
Taylor_1989
402
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
  • #2
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.
 
  • #3
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.
 
  • #4
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:
  • #5
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.
 
  • #6
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.
 
  • #7
blue_leaf77 said:
since key actually has trailing 251 blank spaces.
Yep. I forgot that key was declared to hold 256 characters.
 
  • #8
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.
 
  • #9
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]
 

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

What is the purpose of "Repeat a string in Fortran"?

The purpose of "Repeat a string in Fortran" is to create a program that can repeat a given string a certain number of times in the Fortran programming language. This can be useful for tasks such as generating output or formatting text.

How do you repeat a string in Fortran?

To repeat a string in Fortran, you can use the REPEAT function. This function takes in two arguments: the string you want to repeat and the number of times you want it to be repeated. The function then returns the repeated string as the output.

Can you repeat a string a variable number of times in Fortran?

Yes, you can repeat a string a variable number of times in Fortran by using a variable as the second argument in the REPEAT function. This allows you to dynamically change the number of times the string is repeated based on user input or other conditions in the program.

Are there any limitations to repeating a string in Fortran?

Yes, there are some limitations to repeating a string in Fortran. The REPEAT function can only repeat a string up to 255 times. Additionally, the string being repeated cannot be longer than 255 characters. If these limitations are exceeded, the program may encounter errors or unexpected behavior.

Is there a way to repeat a string without using the REPEAT function in Fortran?

Yes, there are alternative ways to repeat a string in Fortran without using the REPEAT function. One way is to use a DO loop to iterate through the string and print it multiple times. Another option is to concatenate the string with itself multiple times using the // operator. However, the REPEAT function is the most efficient and straightforward method for repeating a string in Fortran.

Similar threads

  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
11
Views
10K
Back
Top