当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言50套程序修改上机题(38)
发布时间:2011/11/1 10:05:06 来源:城市学习网 编辑:ziteng

  38、给定程序MODI1.C中函数fun的功能是:从s所指字符串中,找出与t所指字符串相同的子串的个数作为函数值返回。

      例如,当s所指字符串中的内容为:"abcdabfab",t所指字符串的内容为:"ab",则函数返回整数3。

      请改正程序中的错误,使它能得出正确的结果。

      注意:不要改动main 函数,不得增行或删行,也不得更改程序的结构!

  #include 

  #include 

  int fun (char  *s,  char *t)

  {

    int   n;      char  *p , *r;

    n = 0;

    while ( *s )

    {   p = s;   r = t;

        while ( *r )

          if ( *r == *p )  {

  /************found************/

            r++;  p++

          }

          else  break;

  /************found************/

        if ( r == '\0' )

          n++;

        s++;

    }

    return  n;

  }

  main()

  {

    char   s[100],  t[100];     int   m;

    printf("\nPlease enter string S:"); scanf("%s", s);

    printf("\nPlease enter substring t:"); scanf("%s", t);

    m = fun( s,  t);

    printf("\nThe result is:  m = %d\n", m);

  }

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved