zoukankan      html  css  js  c++  java
  • 从一篇文章中检查特定单词出现数量和第一次出现位置

     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] != '')
    22     {
    23         do
    24         {
    25             if(word[j] == '' || word[j] != content[i+j])
    26             {
    27                 k=j;
    28                 j=0;
    29                 break;
    30             }
    31             j++;
    32         }
    33         while(word[j] != '');
    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
    first position: %d
    ", discovedCnt, firstPos-strlen(word));
    46     return 0;
    47 }
    48 
    49 int strlen(char *pointer)
    50 {
    51     int length=0;
    52     while(*pointer != '')
    53         pointer++;
    54     return length;
    55 }
    56 
    57 void changeLaggerToSmaller(char *pointer)
    58 {
    59     while (*pointer != '')
    60     {
    61         if (*pointer >= 'A' && *pointer <= 'Z')
    62             *pointer = *pointer + 32;
    63         pointer++;
    64     }
    65 }

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

  • 相关阅读:
    Hive学习笔记记录
    Hadoop学习笔记记录
    python学习笔记记录
    2018高级软件工程——助教总结
    Week3 第二次结对编程
    Week2 第一次结对编程
    Week1 博客作业
    最后一周总结
    阅读和提问3
    个人项目 案例分析
  • 原文地址:https://www.cnblogs.com/preacher/p/6349854.html
Copyright © 2011-2022 走看看