基于visual Studio2013解决面试题之0905子串数量




题目



解决代码及点评

/*
    已知一个字符串,比如 asderwsde, 寻找其中的一个子字符串比如 sde 的个数,如果没有返回 0 ,
    有的话返回子字符串的个数

	解决办法:简单遍历即可
*/

#include <iostream>
using namespace std;

// 查找子串
int SubStrCount(char *pszBuf, char *pszSub)
{
    int i, j;
    int nLen1 = strlen(pszBuf);
    int nLen2 = strlen(pszSub);
    int nCount = 0;

	// 遍历
    for (i = 0; i < nLen1; i++)
    {
        j = 0;
        int nTmpPos = i;
        if (pszBuf[i] == pszSub[j] && i < nLen1 && j < nLen2)
        {
            i++;
            j++;
			// 比对
            while (pszBuf[i] == pszSub[j] && i < nLen1 && j < nLen2 )
            {
                i++;
                j++;
            }

			// 如果成功则计数加1
            if (j == nLen2)
            {
                nCount++;
                i--;
            }
            else
            {
                i--;
            }
        }
    }

    return nCount;
}
int main()
{
    char szBuf[] = "sdsdesderwssede";
    char szSub[] = "sde";
    cout<<SubStrCount(szBuf, szSub)<<endl;
    system("pause");
    return 0;
}

代码下载及其运行

代码下载地址:http://download.csdn.net/detail/yincheng01/6704519

解压密码:c.itcast.cn


下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:

1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”


2)在下拉框中选择相应项目,项目名和博客编号一致

3)点击“本地Windows调试器”运行


程序运行结果









posted on 2013-12-20 01:20  三少爷的剑123  阅读(154)  评论(0编辑  收藏  举报

导航