return string;
}
字符串比较
int strlen(char *string){
int i;
for(i=0;string[i]!='\0';i++){}
return i;
}
void strcmp(char *string,char *string2){
int s_len;
int d_len;
int i,j;
s_len=strlen(string);
d_len=strlen(string2);
if(s_len!=d_len){
printf("this string no enque\n");
}else{
for(i=0;i<s_len;i++){
if(string[i]==string2[i]){
printf("%dsame\n",i+1);
}else{
j=1;
printf("%dno same\n",i+1);
}
}
if(j==1){
printf("this string no enque\n");
}else{
printf("this string enque\n");
}
}}
字符串截取
char *substr(char *string,int num,int len){
int i,j;
for(i=num,j=0;i<num+len;i++,j++){
string[j]=string[i];
}
string[j]='\0';
return string;
}
结构体求分数平均值:
#include <stdio.h>
#include <stdlib.h>
int main(){
int num;
int i,j;
int sum_math,sum_english,sum_chinese;
sum_math=sum_english=sum_chinese=0;
struct grade {
float math;
float english;
float chinese;
};
printf("please enter stduent num:\n");
scanf("%d",&num);
struct grade *score;
score=(struct grade *)malloc(num*sizeof(struct grade));
if(!score){
printf("erro");
}else{
for(i=0;i<num;i++)
{
printf("please enter student %d\n",i+1);
printf("please enter math score:\n");
scanf("%f",&score[i].math);
sum_math=sum_math+score[i].math;
printf("please enter english score:\n");
scanf("%f",&score[i].english);
sum_english=sum_english+score[i].english;
printf("please enter chinese score:\n");
scanf("%f",&score[i].chinese);
sum_chinese=sum_chinese+score[i].chinese;
}
}
printf("this student math ave:\n");
printf("%f\n",(float)sum_math/(float)num);
printf("this student english ave:\n");
printf("%f\n",(float)sum_english/(float)num);
printf("this student chinese ave:\n");
printf("%f\n",(float)sum_chinese/(float)num);