hello, I'm working on a program to encrypt using Caesar cipher, but the encryption I am receiving one letter only. For example, I input (car) but i receive only the letter (F).

3 visualizzazioni (ultimi 30 giorni)
clc
clear all;
close all;
p=input('enter plain text: ', 's')
p=lower(p);
lp=size(p);
for i=1:lp
c(i)=int16(p(i))-97+3;
end
c=mod(c,26)+65;
c=char(c)

Risposte (1)

Voss
Voss il 24 Dic 2022
Modificato: Voss il 24 Dic 2022
lp is a 1-by-2 vector, only the first element of which is used in a colon expression (as you are using it, in defining your for loop). So if p is a row vector, the size of p is 1-by-something, so lp will be [1 something], and the for loop iterates once.
Instead of using size, use numel.
Consider the difference between this:
p = 'car';
lp = size(p)
lp = 1×2
1 3
for i = 1:lp
disp(['Iteration ' num2str(i)]);
end
Iteration 1
And this:
p = 'car';
lp = numel(p)
lp = 3
for i = 1:lp
disp(['Iteration ' num2str(i)]);
end
Iteration 1 Iteration 2 Iteration 3

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by