1. 程式人生 > >Linux C substring 子字串

Linux C substring 子字串

#include <stdio.h>
#include <string.h>

char * substring(char * s_src, int i_start, int i_end){
 char * p_stmp;
 int substrlength = i_end - i_start + 1;

 printf ("Malloc size:%i\n",substrlength);

 p_stmp = (char *) malloc(substrlength);

 strncpy(p_stmp,s_src + i_start, substrlength);

 return p_stmp;
}

int main(){
 char s[] = "0123456789";
 char * s_tmp;
 int start = 3, end = 6;

 s_tmp = substring (s,3,6);
 printf("s_tmp:%s\n",s_tmp);
 free(s_tmp);

 return 0;
}