Write a program that enters 5 names of towns and their respective distance (an integer) from London in miles. The program will print of the names of the towns that are less than 100 miles from London. Use arrays and character strings to implement your program. 例如在这题中,如果你考虑城市的名称中有空格的存在,那么你就不应该用sacnf去读取城市的名称,你应该用gets去读取。但是gets会把回车吃掉。举个例子。如果你这么写。
#include<stdio.h> intless100(int miles); intmain() { char name[10000]; int distance[5]; int i,a=0; for(i=0;i<5;i++) { printf("Please Enter Name : "); gets(name); printf("Please Enter the distance:"); scanf("%d",&distance[i]); a = less100(distance[i]); if (a == 1) printf("\n%s is less than 100 miles from London\n\n",name); } return0; }
intless100(int miles){ if (miles < 100) return1; return0; }
#include<stdio.h> intless100(int miles); intmain() { char name[10000]; int distance[5]; int i,a=0;
for(i=0;i<5;i++) { printf("Please Enter Name : "); gets(name); printf("\n"); fflush(stdin); //清空缓冲区 printf("Please Enter The Distance From London : "); scanf("%d",&distance[i]); printf("\n"); fflush(stdin); //清空缓冲区 a = less100(distance[i]); if (a == 1) printf("%s\n\n",name); } return0; }
intless100(int miles){ if (miles < 100) return1; return0; }
可以说字符串就是数组,数组就是字符串。如果想定义二维数组的字符串可以像这样定义char string[5][10000]; string[0]就是第一个字符串,string[1]是第二个字符串,依次类推到string[4],长度均为10000。 还有就是strlen和sizeof的区别,由于字符串都会以”\0”结尾,所以真实长度比你输入的长度多1。例如字符串’Happy’的长度为6,分别为 H a p p y \0 。 所以如果用strlen取字符串长度会得到5,也就是不会读取 \0 , 但是如果用sizeof会得到6 , 也就是算上了\0。 使用时要格外注意。
//Check if the input is valid int k=0; printf("Please Enter Your ID Number:"); do{ if(k>0) printf("Your ID Number Includes 2 Letters and 4 Digits:"); k++; scanf("%s",ID); }while(strlen(ID)!=6||valid(ID)!=1); //If the length is not 6, it will ask to put again, and the speed will be fast
intvalid(char *string){ int i,letter=0,number=0; for(i=0;i<2;i++) { if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z')) //How many letters letter+=1; } for(i=2;i<6;i++) { if(string[i]>='0' && string[i]<='9') //How many numbers number+=1; } if(letter == 2 && number == 4) //If the string is valid, return 1 return1; return0; //If it is invalid, return 0 }
FILE *record; readfile(record);//调用函数 voidreadfile(FILE *record) { char *str1,*ptr; char *res[999]; int p = 0; FILE *pFile; char mystring[1000]; pFile = fopen("record.txt","r"); if (pFile == NULL) perror ("Error opening file"); else { while (fgets (mystring , 100 , pFile) != NULL ) //读取长度为100 { int len = strlen(mystring); //Read the record.tet by line if(mystring[len-1]=='\n') //Think of a line as a string mystring[len-1] = '\0'; //按行读取
char* tmp = (char*)malloc(100*sizeof(char)); memcpy(tmp,mystring,len); //usage memcpy(dest, src, strlen(src)); res[p++] = tmp; for(i=0;i<p;i++)//如果想打印文件,可以直接printf("%s",res[i]); { ptr = strstr(res[i],ID); //strstr用来判断前面的字符串是否包含后面字符串的内容。 } //Determine whether the string containing the ID if( ptr != NULL ) //If it does contain the string of ID printf("%s\n",ptr); //Printf the string } fclose (pFile); //Close the file } }