strlen实现
#include <stdio.h>
#include <assert.h>
int my_strlen(char *p)
{
int i=0;
assert(p!=NULL);
while (*(p+i)!='\0')
{
i++;
}
return i;
}
int main()
{ char a[10]="abcdefj";
char b[10];
printf("%d",my_strlen(a));
return 0;
}
strcpy实现
#include <stdio.h>
#include <assert.h>
char *my_strcopy(char *p,const char *t)
{
int i=0;
assert(p!=NULL);
while (*(t+i)!='\0')
{ *(p+i)=*(t+i);
i++;
}
*(p+i)='\0';
return p;
}
int main()
{ char a[10]="abcdefj";
char b[10]="xxxxxxxxxx";
printf("%s",my_strcopy(b,a));
return 0;
}
调整数组使奇数全部都位于偶数前面
#include <stdio.h>
#include <assert.h>
int *my_strcopy(int *p,const int *t)
{
int i=0;
assert(p!=NULL);
while (*(t+i)!='\0')
{ *(p+i)=*(t+i);
i++;
}
*(p+i)='\0';
return p;
}
void setarr(int* p,int* t)
{
my_strcopy(p, t);
int i,j;
for(i=0,j=0;i<=10;i++)
{
if(*(p+i)%2==1)
{
*(t+j)=*(p+i);
j++;
}
}
for (i=0; i<=10; i++)
{
if (*(p+i)%2==0)
{
*(t+j)=*(p+i);
j++;
}
}
}
int main()
{ int a[10]={1,3,2,4,5,6,7,8,5,9};
int b[10];
int i=0;
setarr(b, a);
for (i=0; i<=9; i++)
{
printf("%d",a[i]);
}
return 0;
}