090907c语言学习日记时间:2022-08-29 21:39:58#include<stdio.h> #include<string.h> #define SIZE 81 #define LIM 100 #define STOP "quit" int main(void) { char input [LIM][SIZE]; int ct=0; printf("Enter up to %d lines(type)quit to quit ):/n",LIM); while(ct<LIM&&gets(input[ct])!=NULL&& strcmp(input[ct],STOP)!=0) { ct++; } printf("%d strings entered /n",ct); getch(); return 0; } //strncmp函数的用法 #include<stdio.h> #include<string.h> #define LISTSIZE 5 int main(void) { char *list[LISTSIZE]={ "astronomy","astounding", "astrophysics","ostracize", "asterism"}; int count=0; int i; for(i=0;i<LISTSIZE;i++) { if(strncmp(list[i],"astro",5)==0) { printf("Found:%s/n",list[i]); count++; } } printf("The list contained %d words beginning with astro./n",count); getch(); return 0; } //复制字符串 #include<stdio.h> #include<string.h> #define SIZE 40 #define LIM 5 int main(void) { char qwords[LIM][SIZE]; char temp[SIZE]; int i=0; printf("Enter %d words beginning with q:/n",LIM); while(i<LIM&&gets(temp)) { if(temp[0]!='q') printf("%s doesn't begin with q!/n",temp); else { strcpy(qwords[i],temp); i++; } } puts("Here are the words accepted:"); for(i=0;i<LIM;i++) puts(qwords[i]); getch(); return 0; } #include<stdio.h> #include<string.h> #define WORDS "beats" #define SIZE 40 int main(void) { char *orig=WORDS; char copy[SIZE]="Be the best that you can be"; char *ps; puts(orig); puts(copy); ps=strcpy(copy+7,orig); puts(copy); puts(ps); getch(); return 0; } //strncpy #include<stdio.h> #include<string.h> #define SIZE 40 #define LIM 5 #define TAGESIZE 7 int main(void) { char qwords[LIM][TAGESIZE]; char temp[SIZE]; int i=0; printf("ENter %d words with q:/n",LIM); while(i<LIM&&gets(temp)) { if(temp[0]!='q') { printf("%s doesn't begin with q/n",temp); } else { strncpy(qwords[i],temp,TAGESIZE-1);//注意这里 qwords[i][TAGESIZE-1]='/0'; i++; } } puts("Here are the words accepted:"); for(i=0;i<LIM;i++) puts(qwords[i]); getch() ; return 0; } //format格式化一个字符串,sprintf #include<stdio.h> #define MAX 20 int main(void) { char first[MAX]; char last[MAX]; char formal[2*MAX+10]; double prize; puts("Enter your first name:"); gets(first); puts("Enter your last name:"); gets(last); puts("Enter your prize money:"); scanf("%lf",&prize); sprintf(formal,"%s,%-19s:$%6.2f/n",last ,first,prize); puts(formal); getch(); return 0; } //排列字符串 #include<stdio.h> #include<string.h> #define SIZE 81 #define LIM 20 #define HALT " " void stsrt(char *strings[],int num); int main(void) { char input[LIM][SIZE]; char *ptstr[LIM]; int ct=0; int k; printf("Input up to %d lines.and I will sort them./n",LIM); printf("To stop,press the Enter key at a line's start./n"); while(ct<LIM&&gets(input[ct])!=NULL&&input[ct][0]!='/0') { ptstr[ct]=input[ct]; ct++; } stsrt(ptstr,ct); puts("/nHere's the sorted list./n"); for(k=0;k<ct;k++) { puts(ptstr[k]); } getch(); return 0; } void stsrt(char *strings[],int num) { char *temp; int top,seek; for(top=0;top<num-1;top++) { for(seek=top+1;seek<num;seek++) { if(strcmp(strings[top],strings[seek])>0) { temp=strings[top]; strings[top]=strings[seek]; strings[seek]=temp; } } } }