在这里插入代码片
一:让字符数组中一个单位存一个字符串而不是一个字符
#include <stdio.h>
#include <stdlib.h>
#define N 100
#define M 5
int main()
{
char *s[M];
int i,j;
for(i=0; i<M; i++){
s[i] = (char *)malloc((N+1)*sizeof(char));
printf("输入:\n");
gets(s[i]);
printf("输出\n");
puts(s[i]);
for(j=0;j<strlen(s[i]);j++)
printf("字符:%c ",*(s[i]+j));
printf("字符串%s\n",s[i]);
printf("\n");
}
for(i=0; i<M; i++){
free(s[i]);
}
return 0;
}
二:比较字符串的大小并找出其最大值
(其中的大小比较中找出最值的方法值得参考)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char str[80], max[80];
int k;
scanf("%s", &max);
for(k=1;k<=4;k++)
{
scanf("%s", &str);
if(strcmp(max,str)<0)
strcpy(max,str);
}
printf("Max is: %s\n", max);
}
三:随机不重复函数
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main()
{
int arr[5] = {5,12,13,8,10},i,w,t,n=5;
srand((unsigned short)time(NULL));
for (i=0;i<5;i++)
{ w=rand()%(n-i)+i;
t=arr[i];
arr[i]=arr[w];
arr[w]=t;
}
for(i = 0;i<5;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
四:冒泡排序
#include<stdio.h>
int main()
{
int a[15]={2,3,4,5,47,15,36,26,27,44,46,38,19,50,48},i,j,temp;
for(i=0;i<15;i++)
{
for(j=1;j<15-i;j++)
{
if(a[j-1]>a[j])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
printf("%d ",a[i]);
}
return 0;
}
五:选择排序
#include<stdio.h>
int main()
{
int a[15]={2,3,4,5,47,15,36,26,27,44,46,38,19,50,48},i,j,temp;
for(i=0;i<15;i++)
{
for(j=i+1;j<15;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%d ",a[i]);
}
return 0;
}