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

MATLAB project to decrypt messages

  1. Sep 29, 2007 #1
    Hi everyone. I have a MATLAB project to decrypt messages using only uppercase characters and spaces(i.e no punctuation). The problem is that my program is working but does not print spaces, instead of a space it prints 7. If anyone can help, I'll be very greatful.Here is my program:

    a1 = input('Please enter a sentence: ','s');
    p = upper(a1); % Change the text to uppercase
    double(p); % String to ASCII codes

    for i = 1:numel(p) % Iterate as long as there are characters
    % in the string
    if (p(i) ~= 32) & (p(i) > 67)
    p(i) = p(i) - 3;
    elseif (p(i) <= 67)
    p(i) = p(i) + 23;
    else
    * (p(i) == 32)
    end
    end
    disp(p)

    * This part seems to be useless.
    If the user inputs: khoor pb iulhqg
    output: HELLO7MY7FRIEND
     
  2. jcsd
  3. Sep 29, 2007 #2
    Just replace any occurence of '7' in p with ' ' before disp(p):

    for i=1:numel(p)
    if(p(i)=='7')
    p(i)=' ';
    end
     
  4. Sep 29, 2007 #3
    Your problem appears to be this part:

    Code (Text):

    elseif (p(i) <= 67)
      p(i) = p(i) + 23;
     
    ...it converts 32 (' ') into 32 + 23 = 55 ('7').

    If you don't want to convert spaces into anything then use an outermost IF, something like this:

    Code (Text):

    if (p(i) ~= 32)
      if (p(i) > 67)
        p(i) = p(i) - 3;
      else
        p(i) = p(i) + 23;
      end
    end
     
    (I'm unsure of the syntax but you should get the idea.)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: MATLAB project to decrypt messages
  1. Error message (Replies: 0)

  2. Annoying debug messages (Replies: 13)

Loading...