#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* substr(char* s, int pos, int len)
{
char* t=(char*)malloc(sizeof(char)*(len+1));
int str_len=strlen(s);
if(pos<=0 || pos>str_len || (pos+len-1)>str_len || len<1)
{
return "Error!";
}
int i=pos-1,j=0;
for(j;j<len;j++)
{
t[j]=s[i++];
}
t[j]='\0';
return t;
}
int main()
{
printf("\nsubstr(\"Microsoft\",5,3)=%s",substr("Microsoft",5,3));
printf("\nsubstr(\"Microsoft\",2,15)=%s",substr("Microsoft",2,15));
return 0;
}
温馨提示:答案为网友推荐,仅供参考