求助,关于打断字符无法实现的问题。
编程如下:#include <cstdlib>
#include <iostream>
using namespace std;
int SplitString(char dest,
char token,
char *str1,
char *str2)
{
assert( dest );
assert( str1 );
assert( str2 );
int index=0;
bool befToken = true;
while(*dest != '\0')
{
if(*dest !=token)
{
if(befToken)
{
index++;
*(str1++)=*(dest++);
}
else
{
*(str2++)=*(dest++);
}
}
else
{
befToken = false;
dest++;
}
}
*str1 = '\0';
*str2 = '\0';
return index;
}
int main(int argc, char *argv[])
{
cout<<"-----分割字符串-----"<<endl;
const int LEN = 16;
cout<<"请输入字符串:"<<endl;
char dest[LEN]={'\0'};
cin>>dest;
cout<<"分界请输入一个字符:"<<endl;
char token = "";
cin>>token;
cout<<endl;
char str1[LEN]={'\0'};
char str2[LEN]={'\0'};
int index = SplitString( dest, token,
str1,str2);
cout<<"字符串在位置 "<<index
<<"处分割成两个字符串:"<<endl;
cout<<str1<<'\t'<<str2<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
反复试了几遍,都无法实现,请人指教。
[此贴子已经被作者于2017-12-17 12:56编辑过]