作业十二 字符串和结构
2016-12-11 21:28 zlt.Santorini'Ly 阅读(166) 评论(0) 编辑 收藏 举报找出最长的字符串
分类统计字符个数
#include<stdio.h> int main() { int a,zm=0,kg=0,sz=0,qt=0; while((a=getchar())!='\n') { if((a>='A'&&a<='Z')||(a>='a'&&a<='z')) zm++; else if(a==' ') kg++; else if(a>='0'&&a<='9') sz++; else qt++; } printf("英文字母的个数:%d\n",zm); printf("空格的个数:%d\n",kg); printf("数字的个数:%d\n",sz); printf("其他字符的个数:%d\n",qt); return 0; }
学生信息管理系统
#include<stdio.h> #include<string.h> struct students_list{ char name[10]; int chinese,math,english; char ID[13]; double average; }; int Count=0; void new_student(struct students_list students[]); void search_student(struct students_list students[],char *name); void print_student(struct students_list students[]); int main() { int choice; char name[10]; struct students_list students[50]; do{ printf("******学生信息查询******\n"); printf("\t1:输入\n"); printf("\t2:查询\n"); printf("\t3:输出\n"); printf("\t0:退出\n"); printf("请选择功能:"); scanf("%d",&choice); switch(choice){ case 1: new_student(students); break; case 2: printf("请输入要查找的学生姓名:"); scanf("%s",name); search_student(students,name); break; case 3: print_student(students); break; case 0: break; } }while(choice!=0); printf("谢谢使用学生信息查询系统!\n"); return 0; } void new_student(struct students_list students[]) { struct students_list f; if(Count==50){ printf("信息列表已满!\n"); return; } printf("请输入新学生的姓名:"); scanf("%s",f.name); printf("请输入新学生的语文成绩:"); scanf("%d",&f.chinese); printf("请输入新学生的数学成绩:"); scanf("%d",&f.math); printf("请输入新学生的英语成绩:"); scanf("%d",&f.english); printf("请输入新学生的学号:"); scanf("%s",f.ID); students[Count]=f; Count++; } void search_student(struct students_list students[],char *name) { int i,flag=0; if(Count==0){ printf("学生信息列表是空的!\n"); return; } for(i=0;i<Count;i++) if(strcmp(name,students[i].name)==0){ flag=1; break; } if(flag){ printf("姓名:%s\t",students[i].name); printf("语文成绩:%d\t",students[i].chinese); printf("数学成绩:%d\t",students[i].math); printf("英语成绩:%d\t",students[i].english); printf("平均成绩:%.2lf\t",students[i].average=(students[i].chinese+students[i].math+students[i].english)/3.0); printf("学号:%s\n",students[i].ID); } else printf("无此学生信息!"); } void print_student(struct students_list students[]) { printf("学生信息列表的学生信息如下:\n"); int i; for(i=0;i<Count;i++) { printf("%s\t%d\t%d\t%d\t%.2lf\t%s\t\n",students[i].name,students[i].chinese,students[i].math,students[i].english,students[i].average,students[i].ID); } }
分类统计字符个数方法二
#include<stdio.h> int main() { int digit,capital,letter,blank,other; char s[80]; int i; digit=capital=letter=blank=other=0; printf("Enter characters:"); gets(s); i=0; while(s[i]!='\0') { if(s[i]>='a'&&s[i]<='z') letter++; else if(s[i]>='A'&&s[i]<='Z') capital++; else if(s[i]>='0'&&s[i]<='9') digit++; else if(s[i]==' ') blank++; else other++; i++; } printf("大写字母的个数为:%d\n",capital); printf("小写字母的个数为:%d\n",letter); printf("数字的个数为:%d\n",digit); printf("空格的个数为:%d\n",blank); printf("其他字符的个数为:%d\n",other); return 0; }
找出最长的字符串修正
#include<stdio.h> #include<string.h> int main() { int i; char sx[80],smlen[80]; printf("请输入5个字符串:"); scanf("%s",sx); strcpy(smlen,sx); for(i=1;i<5;i++) { scanf("%s",sx); if(strlen(smlen)<strlen(sx)) strcpy(smlen,sx); } printf("maxlen is %s\n",smlen); return 0; }