Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/arrays/cipher by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
<p>Write a program to receive a character string input (no spaces) and then "encrypt" the string using a substitution cipher given below:</p>
<pre>
abcdefghijklmnopqrstuvwxyz
gkihjlmaefnzyqbcdrstuopvwx
</pre>
<p>
Thus if the input string was <tt>"helloworld"</tt>
After encryption you would output <tt>"ajzzbpbrzh"</tt>
</p>
<p>We have defined another array to help you:</p>
<pre>
char cipher[27] = "ghijklmaefnzyqbcdrstuopvwx";
</pre>
<p>How could we use the original character to index ("look-up" a value in) the 
   cipher array (use the cipher array as a LUT)? Recall that ASCII characters
   are just numbers:  <tt>a</tt>=<tt>97</tt>, <tt>b</tt>=<tt>98</tt>, etc. 
   But try to write you code in a way that doesn't require knowledge of the
   specific underlying numbers.</p>
<p>Note: we have used a library function <tt>strlen()</tt> to find the length of the string entered.</p>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/arrays/examples/arraybad (author: redekopp@usc.edu)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <cstring>
using namespace std;
int main() {
   char instr[40];
   char outstr[40];
   char cipher[] = "gkihjlmaefnzyqbcdrstuopvwx";
   
   cin >> instr;
   int len = strlen(instr);   
\hide[
   for(int i=0; i < 40; i++){
      outstr[i] &= 0x7f;
   }   
]\
   
\[
   for(int i=0; i < len; i++){
      outstr[i] = cipher[instr[i] - 'a'];
   }
   outstr[len] = '\0';   
]\
   
   cout << outstr << endl;
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"stdin":"helloworld"},
   {"stdin":"aabbccddeewwxxyyzz"}
]
Solution visibility remove
Is example?
i.e., just a demo
remove
Add compiler flag(s)
json list of flags
e.g. ["-Wno-unused-variable"]
 
["-Wno-array-bounds"]
remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.