title: Encryption Cesar hard to make loop
body: <p>I have a program to do for school where a decryption should be made. It's really simple encryption-style where should only move the letters some steps. For example if you have 3 steps, then the 'A' becomes 'D' and 'G' becomes 'J'. They way I solve this problem is by looking ah the fact that the English languages most common letter is 'E'. So I check for common letters in the text and assume that they represent 'E'. If it is not the most frequent letter then it should be the second most frequent or third etc..It also uses uppercase letters for easier identification with ASCII system. Problem is that in my first text, the roll is not 5 or 10 (which i found with the second most frequent letters) but something else, and I cannot find how to make a nice loop of it, so now it looks real ugly.
  Thanks for the help</p>
  
  <pre><code>clear all;
  close all;
  clc;
  
  %Opens txt file
  fid=fopen('krypteradett.txt','rt');
  text = fscanf(fid,'%c',Inf)
  
  %Makes it uppercase
  txtversal = upper(text);
  
  %checks unique letters and removes unneeded spaces
  bokst = unique(txtversal);
  bokst = strtrim(bokst);
  
  c = histc(txtversal, bokst);
  cdescend = sort(cd,'descend');
  
  
  %Prints out each unique letter and how many times they are repeated
  fprintf('"%c" is %n times\n', [bokst; c])
  
  %Finds the most frequent letter
  storsta = max(max(cd));
  ix=find(c==max(c));
  frekvbokst = bokst(ix);
  fprintf('The most frequent letter is "%cd" and can be found %d times.\n', [frekvbokst; storsta])
  
  %Since 'E' is the most common letter in the English language, it calculates
  %how many "rolls" have been made to come to the new letter. If 'G' is the
  %most frequent letter in our text, then it matches how far it is from the
  %letter 'E' in capital ASCII.
  s = double(frekvbokst);
  eiascii = double('E');
  rullning = (s - eiascii);
  fprintf('the roll is %d step.\n', rullning)
  
  textascii = double(txtversal);
  
  %Capital English letter in ASCII only range from 65 to 90, if it is out of
  %range it should jump the whole alphabet (26 letters) forward/backward. And
  %if it is 0, then it should become 26 to keep the spaces. Still needs
  %rework as it works very bad
  T = (textascii &gt;= 65) &amp; (textascii &lt;= 90);
  bratek(T) = (textascii(T) - rullning);
  bratek(bratek &lt; 64) = bratek(bratek &lt; 64)+26;
  bratek(bratek == 64) = bratek(bratek == 64)+26;
  bratek(bratek &gt; 90) = bratek(bratek &gt;90)-26;
  bratek(bratek == 26) = bratek(bratek == 26)-26;
  
  char(bratek)
  
  %Asks if the text made sense
  ratt = input('Is the text reasonable? (1) for yes and (2) for no.  ');
  
  %If the text didn't make any sense, the program does the same thing as
  %before just with the second most frequent letter and matches it to the
  %letter 'E' to see how many rolls have been made.
  if ratt == 2
      nyttstorsta = max(c(c~=max(c)));
      ip=find(c==nyttstorsta);
      nyttfrekvbokst = bokst(ip);
      nyttfrekvbokst = nyttfrekvbokst(1);
      fprintf('The second most frequent letter is "%c" and can be found %d times.\n', [nyttfrekvbokst; nyttstorsta])
  
      s = double(nyttfrekvbokst);
      eiascii = double('E');
      rullning = (s - eiascii);
      fprintf('The roll is %d step.\n', rullning)
  
      textascii = double(txtversal);
  
      T = (textascii &gt;= 65) &amp; (textascii &lt;= 90);
      nyttbratek(T) = (textascii(T) - rullning);
      nyttbratek(nyttbratek &lt; 64) = nyttbratek(nyttbratek &lt; 64)+26;
      nyttbratek(nyttbratek == 64) = nyttbratek(nyttbratek == 64)+26;
      nyttbratek(nyttbratek &gt; 90) = nyttbratek(nyttbratek &gt;90)-26;
      nyttbratek(nyttbratek == 26) = nyttbratek(nyttbratek == 26)-26;
  
  
      char(nyttbratek)
  
      ratt = input('Is the text reasonable? (1) for yes and (2) for no.  ');
  
  elseif ratt == 1
      disp('You have now decrypted the text: ')
      char(bratek)
  end
  
  %Prints out diagram with most frequent letters (still needs rework as it
  %doesn't print out on diagram which are the most frequent ones). It keeps
  %it to the 10 most frequent ones at least.
  pie(cfix)
  </code></pre>