PART 1 ref code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//extract a aubstring from a given string 'src' here
//from the position start to end
//return value: the result substring
char *Substr( char *src, int start, int end );
int main(int argc, char *argv[])
{
char *resstr = NULL;
char *src = "aakld sdkfjlei ksd";
resstr = Substr( src, 1, 3);
printf("%s",resstr);
system("PAUSE");
return 0;
}
char *Substr( char *src, int start, int end )
{
assert( src );
if( start <0 || end >= strlen( src ) )
{
perror("Substr():illegal index!");
return NULL;
}
char *pstr = src+start;
char *substr = NULL;
int index;
substr = (char *) malloc ( (end - start + 1) * sizeof( char ) );
if(!substr)
{
perror("Substr():allocate memeory failed!");
return NULL;
}
for( index =0; index <= end -start; index ++)
substr[ index ] = pstr[ index ];
substr[index] = '\0';
return substr;
}