Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Repeat a string in Fortran

  1. Mar 7, 2017 #1
    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 in to the repeat function but I only get one output of maths and not seven, why is this?

    Code (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(*,*),i


    end program jason

     
     
  2. jcsd
  3. Mar 7, 2017 #2

    blue_leaf77

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  4. Mar 7, 2017 #3

    DrClaude

    User Avatar

    Staff: Mentor

    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.
     
  5. Mar 7, 2017 #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:

    Code (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 alot of information for me to learn from.
     
    Last edited by a moderator: Mar 8, 2017
  6. Mar 8, 2017 #5

    Mark44

    Staff: Mentor

    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 (Text):
    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.
     
  7. Mar 8, 2017 #6

    blue_leaf77

    User Avatar
    Science Advisor
    Homework Helper

    It will be like

    Code (Text):
    Hm
    ea
    lt
    lh
    os

    a
    l
    l
    and so on since key actually has trailing 251 blank spaces.
     
  8. Mar 8, 2017 #7

    Mark44

    Staff: Mentor

    Yep. I forgot that key was declared to hold 256 characters.
     
  9. Mar 8, 2017 #8
    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 cant 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.
     
  10. Mar 8, 2017 #9

    blue_leaf77

    User Avatar
    Science Advisor
    Homework Helper

    In the code in your first post, try replacing
    Code (Text):
    i=repeat(key,7)
    with
    Code (Text):
    i=repeat(key(1:5),7)
     
  11. Mar 8, 2017 #10
    I have done it! finally persistence pays off. So just to make sure my code now look like this

    Code (Text):
     
    program Jason

    implicit none

    integer:: i,z,j
    character*256 :: plaintext, ciphered,d,y
    character*5::key





    plaintext='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 do


    end program jason

    [\code]
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted