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

List of all available Websheets


Viewing cpp/cstrings/strlen_func 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
 
Define a function <tt>strlen</tt> that takes a single character array as input, 
<tt>src</tt>.  <tt>src</tt> is a null-terminated character array. Return
the number of non-null characters in the string.  Example:  "hi" should return
2 while "" should return 0.
Public permissions remove
Remarks
Comments, history, license, etc.
 
Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
// return the length (# non-null characters) of the C-string
// (null-terminated character array) passed as an argument
\[int]\ strlen(\[char src[]]\)
{
\[
   int x = 0;
   while (src[x] != '\0') {
      x++;
   }
   return x;
]\
}
C++ test suite
See manual
 
[
   ["check-function", "strlen", "int", ["char[]"]],
   ["call-function", "strlen", ["\"hi\""]],
   ["call-function", "strlen", ["\"\""]],
   ["call-function", "strlen", ["\"hello world\""]],
   ["call-function", "strlen", ["\"Walk your bike!\""]]
]
Solution visibility 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'.