#include
#include
#define N 200
char* cut(char s[], int m, int n);
int main()
{
int m = 0, n = 0;
char s[N] = { 0 };
char* p = NULL;
printf("请输入一个字符串,长度小于%d:\n", N);
scanf("%s", s);
printf("请输入截取的位置:");
scanf("%d", &m);
printf("请输入截取的长度:");
scanf("%d", &n);
p = cut(s, m ,n);
if ( p )
{
printf("\n你截取到的字符为:\n%s\n\n", p);
}
else
{
printf("\n截取字符串失败!\n\n");
}
return 0;
}
char* cut(char s[], int m, int n)
{
int len = strlen(s);
if ( len<=0 || m<0 || m >= len || n<=0 )
{
return NULL;
}
if ( m+n < len )
{
*(s+m+n) = '\0';
}
return s+m;
}
void* cut(char * s,int m,int n){
int size=strlen(s);
if(m<1){
if(n>=s)return s;
s[n+1]='\0';
return s;
}
if(n<1){
s[0]='\0';
return s;
}
if(s=
return &s[m];
}//不重新分配内存
void * cut2(const char *s,int m,int n){
char *p;
int size=strlen(s),i;
if(m<1){
size=min(size,n);
p=new char[size+1];
for( i=0;i
return p;
}
if(size<=m){
p=new char;
*p='\0';
return p;
}
size=min(size-m,n);
p=new char[size+1];
for( i=0;i
return p;
}
#include
#include
void *cut(char *s, int m, int n)
{
int i, len;
char *tmp;
len = strlen(s);
while(*s != '\0')
{
m--;
s++;
if(m==0)
{
tmp=s; //保留第m个字符的指针
break;
}
}
if(m+n >= len)
{
return tmp;
}
else
{
for(i=0; i{
tmp[i]=*s;
s++;
}
tmp[n]='\0'; //字符串结束标志
return tmp;
}
}
int main()
{
int m, n;
char *ptr;
char buff[20] = {0};
printf("please input a string:");
gets(buff);
printf("please input m, n:");
scanf("%d, %d", &m, &n);
ptr = cut(buff, m, n);
printf("After cut,buff is %s.\n", ptr);
return 0;
}