Puzzle S19

Delete the first N characters from a string

[M-10] Write function that shortens a string by removing its first N characters. Do this by sliding the characters starting at position N left by N positions.

When N is bigger than the length of the string, make the string the empty string. If N is negative or zero, do nothing. Here is a testing function:

#include <stdio.h>
#include <stdlib.h>

void deleteFirstN( char *str, int N )
{
}

struct test
{
  char str[50];
  char N;
};

int main(int argc, char *argv[])
{
  struct test trials[] =
  {
    {"blue berry", 5},
    {"123456789", 0},
    {"123456789", 1},
    {"123456789", 2},
    {"123456789", 3},
    {"123456789", 4},
    {"123456789", 7},
    {"123456789", 8},
    {"123456789", 9},
    {"123456789", -1},
    {"123456789", -8},
    {"1234", 3},
    {"123", 3},
    {"12", 3},
    {"1", 3},
    {"", 3},
 };

  int j;
  for ( j=0; j < sizeof(trials)/sizeof(struct test); j++ )
  {
    printf( "\"%s\" - %d =\t", trials[j].str, trials[j].N );
    deleteFirstN( trials[j].str, trials[j].N );
    printf( "\"%s\"\n", trials[j].str );
  }

  return 0;
}


Answer         Next Page         Previous Page Home