如果输入“abc def gh i d”,结果将是 abc,def,gh,i,d,
要求实现函数: void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr);
[输出] pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
[注意]只需要完成该函数功能算法,中间不需要有任何 IO 的输入输出
示例 输入:“abc def gh i d”
输出:“abc,def,gh,i,d,”
完整验证代码:
#include <iostream>
using namespace std;
class Solution{
public:
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr){
int i=0, j=0;
while(pInputStr[i]==' ')//处理开头为空格
i++;
if(i==lInputLen-1)//全为空格
cout<<"字符串中全为空格";
for( ; i<lInputLen; i++){
if(pInputStr[i]!=' ')//不是空格的字符
pOutputStr[j++]=pInputStr[i];
else if( (pInputStr[i]==' ')&&(pInputStr[i+1]!=' ') )//空格处理
pOutputStr[j++]=',' ;
if( (pInputStr[i]!=' ')&&(pInputStr[i+1]=='\0') )//最后一个字符是字母,加逗号
pOutputStr[j++]=',' ;
}
}
};
int main(){
const int LENGTH=17;
char str1[LENGTH]="abc def gh i d";
//char str1[LENGTH]=" abc ";
//char str1[LENGTH]=" ";
char str2[LENGTH];
Solution solution;
solution.DivideString(str1,LENGTH,str2);
for(int i=0; i<strlen(str2); i++)
cout<<str2[i];
cout<<endl;
return 0;
}