从一篇文章中检查特定单词出现数量和第一次出现位置

 1 #include<stdio.h>
 2 
 3 int strlen(char*);
 4 void changeLaggerToSmaller(char*);
 5 
 6 int main(void)
 7 {
 8     char word[15], content[1024*1024];
 9     int discovedCnt=0, firstPos= -1;
10     int i,j,k;
11 
12     scanf("%s", word);
13     getchar();
14     gets(content);
15 
16     changeLaggerToSmaller(word);
17     changeLaggerToSmaller(content);
18 
19     i=0;
20     j=0;
21     while (content[i] != '\0')
22     {
23         do
24         {
25             if(word[j] == '\0' || word[j] != content[i+j])
26             {
27                 k=j;
28                 j=0;
29                 break;
30             }
31             j++;
32         }
33         while(word[j] != '\0');
34 
35         if(j>0)
36         {
37             discovedCnt++;
38             if(firstPos<0)
39                 firstPos=i;
40             i+=k;
41         }
42         i++;
43     }
44 
45     printf("discoved count: %d\nfirst position: %d\n", discovedCnt, firstPos-strlen(word));
46     return 0;
47 }
48 
49 int strlen(char *pointer)
50 {
51     int length=0;
52     while(*pointer != '\0')
53         pointer++;
54     return length;
55 }
56 
57 void changeLaggerToSmaller(char *pointer)
58 {
59     while (*pointer != '\0')
60     {
61         if (*pointer >= 'A' && *pointer <= 'Z')
62             *pointer = *pointer + 32;
63         pointer++;
64     }
65 }

 

第一个输入是单词 ,第二个输入是文章 ,回车执行控制台读取。 

 

posted @ 2017-01-25 18:02  牧师/preacher  阅读(558)  评论(0编辑  收藏  举报