1、 字符串恢复
代码:
#include <stdio.h>
int main(){
char source[]=“lipps”;
char target[]=" “;
int i=0;
for(i=0;i<5;i++){
if(source[i]>‘d’)
{target[i]=source[i]-4;}
else{
target[i]=‘z’-(‘d’-source[i]);
}
}
printf(”%s",target);
return 0;
}
2、 排序
代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
int data[]={5,6,3,21,-1,5,7,-9};
int i,j;
int temp;
for(i=0;i<7;i++)
{
for(j=i+1;j<8;j++)
{
if(data[i]>data[j]){
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
for(i=0;i<8;i++)
{
printf("%d,",data[i]);
}
return 0;
}
3、 矩阵的转置
代码:
#include<stdio.h>
int main()
{
int a[2][3]={{4,5,4},
{2,1,1}};
int b[3][2];
for(int j=0;j<3;j++)
{
for(int i=0;i<2;i++)
{
b[j][i]=a[i][j];
printf("%d\t",b[j][i]);
}
printf("\n");
}
return 0;
}
4、 姓名排序
代码:
#include <stdio.h>
#include <string.h>
int main()
{
char name[100][15]={
“向俊霖”,
“赵晨”,
“李东”
};
char tmp[15];
for (int i=0;i<2;i++)
{
for (int j=i+1;j<3;j++)
{
if (strcmp(name[i],name[j])>0)
{
strcpy(tmp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],tmp);
}
}
}
for (int x=0;x<3;x++)
{
printf("%s\n",name[x]);
}
return 0;
}
5、 单词数量统计
代码:
#include <stdio.h>
#include <string.h>
int main()
{
int sum=0;
char a[1000]=“Almost every child will complain about their parents sometimes. It is natural, because when people stay together for a long time, they will start to have argument. But ignore about the unhappy time, our parents love us all the time. No matter what happen to us, they will stand by our sides. We should be grateful to them and try to understand them.”;
for(int i=0;i<1000;i++)
{
if(a[i]==32)
{
sum=sum+1;
}
if(a[i]==a[i-1]&&a[i-1]==32)
{
sum=sum-1;
}
}
printf("%d",sum+1);
return 0;
}