Cheating, round 1

cheating

The assignment was to write a C function that trims the newline character off a string, if it is present. The function takes a string as an argument, trims it in place, and returns nothing.

Here’s what one student turned in:

void trim(char *s)
char *tr ( char *s )
{
  int i = 0;
  int j = strlen ( s ) - 1;
  int k = 0;

  while ( isspace ( s[i] ) && s[i] != '' )
    i++;

  while ( isspace ( s[j] ) && j >= 0 )
    j--;

  while ( i <= j )
    s[k++] = s[i++];

  s[k] = '';

  return s;
}

Announcement to all students: we teachers can use Google, too.

Advertisements

2 thoughts on “Cheating, round 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s