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

List of all available Websheets


Viewing cpp/cs102/practice13/stringDebug 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>Consider the following code, which has two incorrect (but nearly correct) methods. </p>
<p>The code is supposed to implement two functions, one to "rotate" a string a certain amount of characters
   to the left, and one to "rotate" a string a certain amount of characters to the right.</p>
<p>We recommend you try running the code to find the bugs.  Use cout statements as necessary!
   Correct the functions.</p>
Engine
Template / Reference solution
 
#include <iostream>
#include <string>
using namespace std;
string cycleLeft (unsigned int amount, string orig);
string cycleRight (unsigned int amount, string orig);
int main()
{
  string temp, message;
  cin >> message;
  while(cin >> temp)
     message += " " + temp;
  cout << cycleLeft (12, message) << endl;
  cout << cycleLeft(6, cycleRight(6, message)) << endl;
  return 0;
}
string cycleLeft (unsigned int amount, string orig)
{
   \[
REDACTED
\show:
    string ans = orig;
    char begin = ans[0];
    for (int i = 0; i < amount; i++)
    {
        for (int j = 0; j < ans.size(); j++)
        {
            ans[j-1] = ans[j];
        }
        ans[ans.size()-1] = begin;
    }
    return ans;
    ]\
}
string cycleRight (unsigned int amount, string orig)
{
   \[
REDACTED
\show:
    string ans = orig;
    for (int i = 0; i < amount; i++)
    {
        char end = ans[ans.size()];
        for (int j = ans.size(); j >= 0; j--)
        {
            ans[j+1] = ans[j];
        }
        ans[0] = end;
    }
    return ans;      
    ]\
}
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":"Scramble this string!", "args":[]},
   {"stdin":"Happy holidays!", "args":[]},
   {"stdin":"Debugging is the most important skill!", "args":[]}
]


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'.