Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Repeating characters

Subject: Repeating characters

From: Alice

Date: 23 Mar, 2012 19:16:15

Message: 1 of 4

For some work I have to code a function for the Vigenere cipher, I have to make the keyword the same length as my message. so say my message is 'abcdefg' and the keyword is 'at' i need it to repeat the keyword 3 times then have an extra 'a' on the end so it would be 'atatata'. repmat works but only if the message is a multiple of the key, any ideas how i can do this?

Subject: Repeating characters

From: Roger Stafford

Date: 23 Mar, 2012 20:03:18

Message: 2 of 4

"Alice" wrote in message <jkii5v$k7p$1@newscl01ah.mathworks.com>...
> For some work I have to code a function for the Vigenere cipher, I have to make the keyword the same length as my message. so say my message is 'abcdefg' and the keyword is 'at' i need it to repeat the keyword 3 times then have an extra 'a' on the end so it would be 'atatata'. repmat works but only if the message is a multiple of the key, any ideas how i can do this?
- - - - - - - - - - -
 n = length(message);
 m = length(keyword);
 repkeyword = keyword(mod(0:n-1,m)+1);

Roger Stafford

Subject: Repeating characters

From: dpb

Date: 23 Mar, 2012 20:10:44

Message: 3 of 4

On 3/23/2012 2:16 PM, Alice wrote:
> For some work I have to code a function for the Vigenere cipher, I have
> to make the keyword the same length as my message. so say my message is
> 'abcdefg' and the keyword is 'at' i need it to repeat the keyword 3
> times then have an extra 'a' on the end so it would be 'atatata'. repmat
> works but only if the message is a multiple of the key, any ideas how i
> can do this?

Wrap the logic in a function and use it.

msg='abcdefg';
key='at'

function key=buildkey(msg,base)
   n=fix(length(msg),length(base));
   m=rem(length('abcdefg'),length('at'));
   key=repmat(key,1,n);
   if m>0, key=[key base(1:m)]; end

NB: Air code, untested; salt to suit...

--

Subject: Repeating characters

From: Greg Kilmer

Date: 24 Mar, 2012 04:43:17

Message: 4 of 4

Is the keyword always of length 2 or always at least of length 2 but
shorter than 7?

Thanks, Greg G. Kilmer

http://www.panama-foundations.​com/
http://www.panama-inc.com/

http://www.panamataxes.com/
http://www.invest-in-panama.​com/

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us