在后面一部分if (ch == '\n' || ch == EOF)开始.#include #include int main(void){\x05char ch,filename[128],str[21],line[201];\x05int i = 0,j,k,m,lineNum = 0,isfirst = 0,firstLine = -1,lastLine = -1;\x05FILE *fp;\x05printf("input the file's name

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 22:45:58
在后面一部分if (ch == '\n' || ch == EOF)开始.#include #include int main(void){\x05char ch,filename[128],str[21],line[201];\x05int i = 0,j,k,m,lineNum = 0,isfirst = 0,firstLine = -1,lastLine = -1;\x05FILE *fp;\x05printf(

在后面一部分if (ch == '\n' || ch == EOF)开始.#include #include int main(void){\x05char ch,filename[128],str[21],line[201];\x05int i = 0,j,k,m,lineNum = 0,isfirst = 0,firstLine = -1,lastLine = -1;\x05FILE *fp;\x05printf("input the file's name
在后面一部分if (ch == '\n' || ch == EOF)开始.
#include
#include
int main(void)
{
\x05char ch,filename[128],str[21],line[201];
\x05int i = 0,j,k,m,lineNum = 0,isfirst = 0,firstLine = -1,lastLine = -1;
\x05FILE *fp;
\x05printf("input the file's name and the string:");
\x05scanf("%s%s",filename,str);
\x05printf("--------------------------File content:--------------------\n");
\x05fp = fopen(filename,"r");
\x05if (fp == NULL)
\x05{
\x05\x05printf("can not open file!\n");
\x05\x05exit(1);
\x05}
\x05while feof(fp))
\x05{
\x05\x05ch = fgetc(fp);
\x05\x05line[i] = ch;
\x05\x05i++;
\x05\x05
\x05\x05if (ch == '\n')
\x05\x05{
\x05\x05\x05lineNum++;
\x05\x05\x05putchar(ch);
\x05\x05\x05line[i] = '\0';
\x05\x05\x05i = 0;
\x05\x05}
\x05\x05else if (ch == EOF)
\x05\x05{
\x05\x05\x05lineNum++;
\x05\x05\x05line[i] = '\0';
\x05\x05}
\x05\x05else
\x05\x05{
\x05\x05\x05putchar(ch);
\x05\x05}
\x05\x05//完成输出;
\x05\x05if (ch == '\n' || ch == EOF)
\x05\x05{
\x05\x05\x05for (j = 0,k = 0; line[j] = '\0'; j++)
\x05\x05\x05{
\x05\x05\x05\x05
\x05\x05\x05\x05if ((line[j] == str[k]) || (line[j] == (str[k] - 32)) || (line[j] == (str[k] + 32)))
\x05\x05\x05\x05{
\x05\x05\x05\x05\x05for (m = j; ((line[m] == str[k]) || (line[m] == (str[k] - 32)) || (line[m] == (str[k] + 32)))
\x05\x05\x05\x05\x05\x05&& str[k] = '\0'; k++,m++)
\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05;
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05\x05if (str[k] == '\0')
\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05if (isfirst == 0)
\x05\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05\x05firstLine = lineNum;
\x05\x05\x05\x05\x05\x05\x05isfirst = 1;
\x05\x05\x05\x05\x05\x05}
\x05\x05\x05\x05\x05\x05lastLine = lineNum;
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05\x05else if (line[m] = str[k])
\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05k = 0;
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05}
\x05\x05\x05\x05
\x05\x05\x05}
\x05\x05}
\x05}
\x05putchar('\n');
\x05printf("--------------------------File summary:--------------------\n");
\x05printf("%d lines,first line:%d,last line:%d\n",lineNum,firstLine,lastLine);
\x05return 0;
}

在后面一部分if (ch == '\n' || ch == EOF)开始.#include #include int main(void){\x05char ch,filename[128],str[21],line[201];\x05int i = 0,j,k,m,lineNum = 0,isfirst = 0,firstLine = -1,lastLine = -1;\x05FILE *fp;\x05printf("input the file's name
#include
#include
int main(void)
{
\x05char ch,filename[128],str[21],line[201];
\x05int i = 0,j,k,m,lineNum = 0,isfirst = 0,firstLine = -1,lastLine = -1;
\x05FILE *fp;
\x05
\x05printf("input the file's name and the string:");
\x05scanf("%s%s",filename,str);
\x05printf("--------------------------File content:--------------------\n");
\x05fp = fopen(filename,"r");
\x05if (fp == NULL)
\x05{
\x05\x05printf("can not open file!\n");
\x05\x05exit(1);
\x05}
\x05while (!feof(fp))
\x05{
\x05\x05ch = fgetc(fp);
\x05\x05line[i] = ch;
\x05\x05i++;
\x05\x05
\x05\x05if (ch == '\n')//如果遇到换行
\x05\x05{
\x05\x05\x05lineNum++;\x05\x05//累加行数
\x05\x05\x05putchar(ch);\x05//输出换行符
\x05\x05\x05line[i] = '\0'; //置所在的行的字符串结束
\x05\x05\x05i = 0;\x05\x05\x05//i清零
\x05\x05}
\x05\x05else if (ch == EOF)//如果到文件末尾
\x05\x05{
\x05\x05\x05lineNum++;\x05\x05//累加行数
\x05\x05\x05line[i] = '\0';\x05//置所在的行的字符串结束
\x05\x05}
\x05\x05else\x05\x05\x05//其余字符直接输出到屏幕
\x05\x05{
\x05\x05\x05putchar(ch);
\x05\x05}
\x05\x05//完成输出;
\x05\x05
\x05\x05if (ch == '\n' || ch == EOF)//遇到一行结束或者到文件末尾
\x05\x05{
\x05\x05\x05for (j = 0,k = 0; line[j] != '\0'; j++)
\x05\x05\x05{
\x05\x05\x05\x05//比对行line的字符串是否包含输入字符串str(不区分大小写)
\x05\x05\x05\x05if ((line[j] == str[k]) || (line[j] == (str[k] - 32)) || (line[j] == (str[k] + 32)))
\x05\x05\x05\x05{
\x05\x05\x05\x05\x05for (m = j; ((line[m] == str[k]) || (line[m] == (str[k] - 32)) || (line[m] == (str[k] + 32)))
\x05\x05\x05\x05\x05\x05&& str[k] != '\0'; k++,m++)
\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05;
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05\x05if (str[k] == '\0')
\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05if (isfirst == 0)
\x05\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05\x05firstLine = lineNum;//统计第一次相等所在行
\x05\x05\x05\x05\x05\x05\x05isfirst = 1;
\x05\x05\x05\x05\x05\x05}
\x05\x05\x05\x05\x05\x05lastLine = lineNum;\x05\x05//统计最后一次相等所在行
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05\x05else if (line[m] != str[k])
\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05k = 0;
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05}
\x05\x05\x05\x05
\x05\x05\x05}
\x05\x05}
\x05}
\x05putchar('\n');
\x05printf("--------------------------File summary:--------------------\n");
\x05printf("%d lines,first line:%d,last line:%d\n",lineNum,firstLine,lastLine);
\x05
\x05return 0;
}