首页 > 编程 > C++ > 正文

C++实现在文本中找出某个单词的位置信息

2020-01-26 14:43:24
字体:
来源:转载
供稿:网友

代码很简单,功能也很单一,这里就不多废话了,大家直接看代码吧。

#include <stdio.h>#include <string.h> int main(int argc,char**argv){  char *token = argv[1];  FILE *fp = fopen("./test.txt","a+");  char buf[1024];  char *p;  int s=-1,len=strlen(token),line=0,pos=-1;   while(!feof(fp)){    fgets(buf,sizeof(buf),fp);    line ++;      p = buf;    while(*p){      if(*p==token[0] && s==-1){        s = 0;      }else if(*p==token[s+1]){        s ++;      }else{        s = -1;      }       p++;      if(s==len-1){        printf("(%d,%d)/n",line,p-buf-len+1);        s=-1;      }    }    s=-1;  }}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表