//This program is designed to find how many a string appears in the file named ..txt.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ROUTE "C:\\LICENSE.TXT"//the route of your destination file
#define STR "the" //the string which you want to find in the file
main()
{
FILE *fp;
int i;
int number=0;
int ch;
char *str=STR;
int N=strlen(str);
if((fp=fopen(ROUTE,"r"))==NULL)
{
printf("Open file error!\n");
exit(1);
}
while((ch=getc(fp))!=EOF)//get a char
{
for(i=0;i<N;i++)
{
if(ch!=str[i])//compare one by one
break;
else
{
ch=getc(fp);
}
}
if (str[i]==0)//check the end of the str
{
number++;
}
}
printf("%s appeares %d times",str,number);
}
字符串查找
最新推荐文章于 2022-06-09 14:50:28 发布