• 查找包含字母字符串‘ould’的行


    ah love! could you and i with fate conspire

    to grasp this sorry scheme of things entire,

    would not we shatter it to bits--and then

    re-mould it nearer to the heart's desire!

    产生如下输出:

    ah love! could you and i with fate conspire

    would not we shatter it to bits--and then

    re-mould it nearer to the heart's desire!

    这个程序可以清楚的分成三部分:

    while(还有未处理的行)

         if(该行包含指定的模式)

                       打印该行

    #include  <stdio.h>
    #define MAXLINE 100   /*最大输入行长度*/
    
    int getline (char line[],int max);
    int strindex(char source[],char searchfor[]);
    
    char pattern[]="ould";    /*要查找的模式*/
    
    /*找出所有与模式匹配的行*/
    main()
    {
        char line[MAXLINE];
        int found=0;
    
        while (getline(line,MAXLINE)>0)
            if (strindex(line,pattern)>=0)
            {
                printf("%s",line);
                found++;
            }
            return found;
    }
    
    /*getline:取一行放到s中,并返回该行的长度*/
    int getline(char s[],int lim)
    {
        int c,i;
        i=0;
        while(--lim>0&&(c=getchar())!=EOF&&c!='\n')
            s[i++]=c;
        if (c=='\n')
            s[i++]=c;
        s[i]='\0';
        return i;
    }
    
    /*strindex:返回t在s中的位置,若未找到则返回-1*/
    int strindex(char s[],char t[])
    {
        int i,j,k;
        for (i=0;s[i]!='\0';i++)
        {
            for (j=i,k=0;t[k]!='\0'&&s[j]==t[k];j++,k++)
                ;
            if(k>0&&t[k]=='\0')
                return i;
        }
        return -1;
    }
  • 相关阅读:
    接口测试框架——第五篇-测试用例和运行用例
    接口测试框架——第四篇-url、excel内容等
    flex布局
    JSON 对象 与 字符串 互转
    nginx 拒绝本地ip访问
    supervisord
    工作中小玩意
    nginx 反向代理
    php获取当月天数及当月第一天及最后一天
    Homebrew 备忘
  • 原文地址:https://www.cnblogs.com/ligongye/p/3118492.html
Copyright © 2020-2023  润新知