1、使用冒泡排序法比较大小
//用冒泡排序法比较元素大小
#include <stdio.h>
#define N 5
int input(int *arr)//输入元素
{
int i;
for(i=0;i<N;i++)
{
scanf("%d",&arr[i]);
}
}
int output(int *arr)
{
int i;
for(i=0;i<N;i++)
{
printf("%d",arr[i]);
}
}
int sort(int *arr)//冒泡排序比较大小
{
int i,j;
for(i=0;i<N-1;i++)
{
for(j=0;j<N-i-1;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
int main()
{
int arr[N];
input(arr);
sort(arr);
output(arr);
printf("\n");
return 0;
}
2、strlen函数的封装实现
#include <stdio.h>
char my_strlen(char *p)
{
char *s = p;
//while(*p !='\0')
while(*p++ != '\0')
{
//p++;
}
//return p-s;
return p-s-1;
}
int main()
{
char str[10]="cbdjb";
printf("%d\n",my_strlen(str));
return 0;
}
3、strcat函数封装实现
#include <stdio.h>
char my_strcat(char *p,char *s)
{
while(*p != '\0')
{
p++;
}
while(*s != '\0')
{
*p=*s;
s++;
p++;
}
}
int main()
{
char str1[10]="gggg";
char str2[10]="hhhh";
printf("%s\n",my_strcat(str1,str2));
return 0;
}
4、strcpy函数封装实现
#include <stdio.h>
char my_strcpy(char *p,char *s)
{
while(*s !='\0')
{
*p=*s;
s++;
p++;
}
*p = '\0';
}
int main()
{
char str1[10]="dhs";
char str2[10]="gvd";
my_strcpy(str1,str2);
printf("%s\n",str1);
return 0;
}
5、strcmp函数封装实现
#include <stdio.h>
char my_strcmp(char *p,char *s)
{
while(*p = *s && *p != '\0')
{
p++;
s++;
}
if(*p > *s)
{
printf(">");
}else if(*p = *s)
{
printf("=");
}else
{
printf("<");
}
}
int main()
{
char str1[10]="cbsb";
char str2[10]="cbgh";
printf("%d\n",my_strcmp(str1,str2));
return 0;
}