首页 > 其他 > 详细

华为机试—查找子串个数

时间:2014-12-26 18:38:12      阅读:220      评论:0      收藏:0      [点我收藏+]

输入一个字符串,判断有多少个子串。


输入: asdg ds  dga  

输出:3


#include <iostream>  
#include <string>  
using namespace std;  
  
int num_of_sub(char *str)  
{     
    int len = strlen(str);  
    int count = 0;  
    for(int i= 0;i < len;i++)  
	{
        if(str[i] != ' ')  
        {  
            for(int j = i;j < len;j++)  
                if(str[j] == ' '|| j == len - 1)  
                {  
                    count++; 
					i=j;
                    break;  
                }    
        }    
	}
          
    return count;  
}  
  
int main()  
{  
    char str[100]; 
	gets(str);
    cout <<num_of_sub(str)<< endl;  
      
    return 0;  
}  

技术分享

华为机试—查找子串个数

原文:http://blog.csdn.net/wtyvhreal/article/details/42174803

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!