华为机试—子串分离

题目描述:   
通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。请编写一个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补充一个’,’并将子串存储。 
如果输入“abc def gh i        d”,结果将是abc,def,gh,i,d, 
 
要求实现函数:   
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr); 
 
【输入】  pInputStr:  输入字符串 
                  lInputLen:  输入字符串长度                   
【输出】  pOutputStr:  输出字符串,空间已经开辟好,与输入字符串等长; 
【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出 


示例   

输入:“abc def gh i        d”

输出:“abc,def,gh,i,d,”


#include <iostream>
#include <string>
using namespace std;

//子串分离
void DivideString(const char* pInputStr , long lInputLen , char* pOutputStr)  
{  
	int i,j=0;  
    bool flag;  
    for(i = 0 ; pInputStr[i] == ' ' ; ++i)   //跳过字符串前面的空格  
            continue;  
    flag = true;  
    for(;i < lInputLen ; ++i)  
    {  
		if(pInputStr[i] != ' ')  
        {  
			if(!flag)
				flag = true; 
			pOutputStr[j++] = pInputStr[i];    //将各个子串分离保存下来  
        }  
        else  
        {  
			if(flag)
				pOutputStr[j++] = ',';  
            flag = false;  
        }  
    }  
    pOutputStr[j++] = ',';  
    pOutputStr[j++] = '\0';  
} 

void main()
{
	char s[100];
	char result[100];
	while(gets(s))
	{
		DivideString(s,strlen(s),result);
		cout<<result<<endl<<endl;
	}
	cout<<endl;
}


测试结果,可能想的不周全,欢迎查漏补缺:

华为机试—子串分离_第1张图片

你可能感兴趣的:(华为机试,逗号子串分离,子串分离)